Skip to content

Instantly share code, notes, and snippets.

@apgapg
Created April 9, 2021 07:27
Show Gist options
  • Save apgapg/7a9f286360ddb1d95d5afedb62e05af0 to your computer and use it in GitHub Desktop.
Save apgapg/7a9f286360ddb1d95d5afedb62e05af0 to your computer and use it in GitHub Desktop.
Handling error
abstract class ErrorInterceptor extends Interceptor {
@override
Future onError(DioError err) async {
if (err is NoInternetError) {
return NoInternetError();
} else if (err.type == DioErrorType.response) {
final code = err.response!.statusCode;
if (code == 401) {
await onUnauthorizedError();
return UnauthorizedError();
} else if (code! >= 400 && code < 500) {
return ClientError(
request: err.request,
response: err.response,
type: err.type,
error: err.error,
);
} else if (code == 500) {
return ServerError(
'Server Error: ${err.response!.statusCode}'
'\n\n${err.response!.data ?? err.response!.statusMessage}',
);
} else if (code >= 501 && code < 600) {
return ServerError(
'Server Error: ${err.response!.statusCode}'
'\n\nPlease retry later',
);
}
} else if (err.error is SocketException) {
return UnstableInternetError();
}
return UnknownError(err.toString());
}
Future<void> onUnauthorizedError();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment