How to handle common use cases or errors from the response?
This looks good for 5, 10 functions. But what if our ApiRepository grows considerably.
class ApiRepository {
// [...] Constructor
final ClientChannel channel;
late final AppClient appClient;
/// The only way to handle errors is by passing _handleException in each method.
Future<void> login(String phoneNumber, String password) async {
try {
final req = LoginReq(phoneNumber: phoneNumber, password: password);
await appClient.login(req);
} on GrpcError catch (err) {
return _handleException(err);
}
}
/// The only way to handle errors is by passing _handleException in each method.
Future<void> userMe(String? token) async {
CallOptions? options;
if (token != null) {
options = CallOptions(metadata: <String, String>{
"authorization": token,
});
}
try {
// create request example
final req = UserMeReq();
await appClient.userMe(req, options: options);
} on GrpcError catch (err) {
return _handleException(err);
}
}
_handleException(GrpcError err) {
switch (err.code) {
case StatusCode.invalidArgument:
throw InvalidArgumentException(err.message);
case StatusCode.unauthenticated:
throw UnauthenticatedException(err.message);
// We can continue to handle the necessary cases ...
default:
throw InternalException(err.message);
}
}
}
How to handle common use cases or errors from the response?
This looks good for 5, 10 functions. But what if our ApiRepository grows considerably.