Created
June 13, 2020 09:52
-
-
Save saturov/56b5470f86e906324ac772959f345a69 to your computer and use it in GitHub Desktop.
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 NetworkErrorHandler implements ErrorHandler { | |
@override | |
void handleError(Object error) { | |
Object e; | |
bool isSilent = false; | |
bool isOverlayed = false; | |
if (error is SilentException) { | |
e = error.originalError; | |
isSilent = true; | |
} else if (error is OverlayedException) { | |
e = error.originalError; | |
isOverlayed = true; | |
} else { | |
e = error; | |
} | |
if (e is Error) { | |
e = Exception((e as Error).stackTrace); | |
} | |
Logger.d("NetworkErrorHandler handle error", e); | |
if (e is ConversionException) { | |
handleConversionError( | |
e, | |
isSilent, | |
isOverlayed, | |
); | |
} else if (e is NoInternetException) { | |
handleNoInternetError( | |
e, | |
isSilent, | |
isOverlayed, | |
); | |
} else if (e is HttpProtocolException) { | |
handleHttpProtocolException( | |
e, | |
isSilent, | |
isOverlayed, | |
); | |
} else { | |
handleOtherError( | |
e, | |
isSilent, | |
isOverlayed, | |
); | |
} | |
} | |
void handleConversionError( | |
ConversionException e, | |
bool isSilent, | |
bool isOverlayed, | |
); | |
void handleNoInternetError( | |
NoInternetException e, | |
bool isSilent, | |
bool isOverlayed, | |
); | |
void handleHttpProtocolException( | |
HttpProtocolException e, | |
bool isSilent, | |
bool isOverlayed, | |
); | |
void handleOtherError( | |
Exception e, | |
bool isSilent, | |
bool isOverlayed, | |
); | |
} |
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
/// Стандартная реализация эррор хендлера | |
class StandardErrorHandler extends NetworkErrorHandler { | |
final MaterialMessageController _messageController; | |
final DialogController _dialogController; | |
final SessionInteractor _sessionChangedInteractor; | |
//ignore: unused_field | |
final AuthInteractor _authInteractor; | |
StandardErrorHandler( | |
this._messageController, | |
this._dialogController, | |
this._sessionChangedInteractor, | |
this._authInteractor, | |
); | |
@override | |
void handleOtherError( | |
Exception e, | |
bool isSilent, | |
bool isOverlayed, | |
) { | |
if (e is UserNotFoundException) { | |
_dialogController.showAlertDialog( | |
message: getStrings().userNotFoundText, | |
onAgreeClicked: (_) async { | |
_sessionChangedInteractor?.onLogout(); | |
}, | |
); | |
} else if (e is OtpException) { | |
Logger.d("DEV_ERROR $e"); | |
} else if (e is MessagedException) { | |
_show(e.message, isOverlayed); | |
} else if (e is InvalidSessionException) { | |
_sessionChangedInteractor.onLogout(); | |
} else { | |
if (!isSilent) _show(getStrings().commonErrorText, isOverlayed); | |
} | |
} | |
@override | |
void handleHttpProtocolException( | |
HttpProtocolException e, | |
bool isSilent, | |
bool isOverlayed, | |
) { | |
if (e is ServerHttpException) { | |
if (!isSilent) _show(getStrings().serverErrorMessage, isOverlayed); | |
} else if (e is ClientHttpException) { | |
_handleClientHttpException(e, isSilent, isOverlayed); | |
} | |
} | |
@override | |
void handleConversionError( | |
ConversionException e, | |
bool isSilent, | |
bool isOverlayed, | |
) { | |
if (!isSilent) _show(getStrings().badResponseErrorMessage, isOverlayed); | |
} | |
@override | |
void handleNoInternetError( | |
NoInternetException e, | |
bool isSilent, | |
bool isOverlayed, | |
) { | |
if (!isSilent) _show(getStrings().noInternetConnectionErrorMessage, isOverlayed); | |
} | |
void _handleClientHttpException( | |
ClientHttpException e, | |
bool isSilent, | |
bool isOverlayed, | |
) { | |
final statusCode = e.response.statusCode; | |
if (statusCode == HttpCodes.code403) { | |
if (!isSilent) _show(getStrings().forbiddenErrorMessage, isOverlayed); | |
} else if (statusCode == HttpCodes.code404) { | |
if (!isSilent) _show(getStrings().serverErrorMessage, isOverlayed); | |
} else { | |
if (!isSilent) _show(getStrings().defaultHttpErrorMessage, isOverlayed); | |
} | |
} | |
void _show( | |
String msg, | |
bool isOverlayed, | |
) { | |
if (!isOverlayed) { | |
_messageController.show( | |
msg: msg, | |
msgType: MsgType | |
.commonError, //todo выделить base модуль для всех в стандарте | |
); | |
} else { | |
_messageController.showOverlaySnackbar(msg); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment