Improve error handling and presentation

This commit is contained in:
problematicconsumer
2023-10-02 23:57:20 +03:30
parent e1108dc1a7
commit 81658a1c86
6 changed files with 58 additions and 29 deletions

View File

@@ -15,7 +15,7 @@ extension ErrorPresenter on TranslationsEn {
final err = error.present(this);
return err.type + (err.message == null ? "" : ": ${err.message}");
case DioException():
return error.toString();
return error.present(this);
default:
return null;
}
@@ -27,8 +27,37 @@ extension ErrorPresenter on TranslationsEn {
String? mayPrintError(Object? error) =>
error != null ? _errorToMessage(error) : null;
({String type, String? message}) presentError(Object error) {
if (error case Failure()) return error.present(this);
return (type: failure.unexpected, message: null);
({String type, String? message}) presentError(
Object error, {
String? action,
}) {
final ({String type, String? message}) presentable;
if (error case Failure()) {
presentable = error.present(this);
} else {
presentable = (type: failure.unexpected, message: null);
}
return (
type: action == null ? presentable.type : "$action: ${presentable.type}",
message: presentable.message,
);
}
}
extension DioExceptionPresenter on DioException {
String presentType(TranslationsEn t) => switch (type) {
DioExceptionType.connectionTimeout ||
DioExceptionType.sendTimeout ||
DioExceptionType.receiveTimeout =>
t.failure.connection.timeout,
DioExceptionType.badCertificate => t.failure.connection.badCertificate,
DioExceptionType.badResponse => t.failure.connection.badResponse,
DioExceptionType.connectionError =>
t.failure.connection.connectionError,
_ => t.failure.unexpected,
};
String present(TranslationsEn t) {
return presentType(t) + (message == null ? "" : "\n$message");
}
}