Created
April 9, 2021 07:27
-
-
Save apgapg/7a9f286360ddb1d95d5afedb62e05af0 to your computer and use it in GitHub Desktop.
Handling error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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