Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement custom exceptions #54

Open
obrunsmann opened this issue Jan 29, 2023 · 1 comment
Open

Implement custom exceptions #54

obrunsmann opened this issue Jan 29, 2023 · 1 comment
Labels
type: feature request New feature or request

Comments

@obrunsmann
Copy link
Contributor

Unity SDK throws an ApiResponseException in case there is any issue with the API response including StatusCode and Message. I guess it
would be nice to also group all API Exceptions into one SDK owned class which can be handeled by the user:

  • HTTP socket exception
  • Request timeout
  • Abnormal status code
  • Runtime errors
@ilmalte ilmalte added the type: feature request New feature or request label Oct 22, 2024
@GenildoNogueira
Copy link

GenildoNogueira commented Dec 31, 2024

Hello, I've been waiting for this for a while now.
I'm doing it this way!

[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: gRPC Error (code: 6, codeName: ALREADY_EXISTS, message: Username is already in use., details: [], rawResponse: null, trailers: {})

class AuthRepositoryRemote extends AuthRepository {
final _log = Logger('AuthRepositoryRemote');

final NakamaService _nakamaService;

AuthRepositoryRemote({
required NakamaService nakamaService,
}) : _nakamaService = nakamaService;

@OverRide
Future<Result<void, Failure>> register({
required String username,
required String email,
required String password,
}) async {
try {
_currentSession = await _nakamaService.client.authenticateEmail(
email: email,
username: username,
password: password,
create: true,
);

  return Success(null);
} on Exception catch (e) {
  final error = parseGrpcError(e.toString());
  _log.warning(error['message']);
  return Error(RegisterException(customMessage: error['message']));
} finally {
  notifyListeners();
}

}
}

Map<String, dynamic> parseGrpcError(String error) {
final regex = RegExp(
r'gRPC Error (code: (?\d+), codeName: (?[\w_]+), message: (?.?), details: (?

[.?]), rawResponse: (?.?), trailers: (?{.?}))',
);
final match = regex.firstMatch(error);

if (match != null) {
return {
'code': int.tryParse(match.namedGroup('code')!),
'codeName': match.namedGroup('codeName'),
'message': match.namedGroup('message'),
'details': jsonDecode(match.namedGroup('details')!),
'rawResponse': match.namedGroup('rawResponse') == 'null'
? null
: match.namedGroup('rawResponse'),
'trailers': jsonDecode(match.namedGroup('trailers')!),
};
} else {
throw FormatException('Erro gRPC não está no formato esperado.');
}
}

flutter: [AuthRepositoryRemote] Username is already in use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants