Files
umbrix/lib/domain/failures.dart

35 lines
957 B
Dart
Raw Normal View History

2023-09-06 12:56:30 +03:30
import 'package:dio/dio.dart';
import 'package:hiddify/core/prefs/prefs.dart';
2023-07-06 17:18:41 +03:30
mixin Failure {
2023-08-26 17:01:51 +03:30
({String type, String? message}) present(TranslationsEn t);
2023-07-06 17:18:41 +03:30
}
2023-09-22 14:17:42 +03:30
/// failures ignored by analytics service etc.
mixin ExpectedException {}
2023-07-06 17:18:41 +03:30
extension ErrorPresenter on TranslationsEn {
2023-09-06 12:56:30 +03:30
String? _errorToMessage(Object error) {
switch (error) {
case Failure():
final err = error.present(this);
return err.type + (err.message == null ? "" : ": ${err.message}");
case DioException():
return error.toString();
default:
return null;
2023-08-26 17:01:51 +03:30
}
2023-07-06 17:18:41 +03:30
}
2023-08-26 17:01:51 +03:30
2023-09-06 12:56:30 +03:30
String printError(Object error) =>
_errorToMessage(error) ?? failure.unexpected;
String? mayPrintError(Object? error) =>
error != null ? _errorToMessage(error) : null;
2023-08-26 17:01:51 +03:30
({String type, String? message}) presentError(Object error) {
if (error case Failure()) return error.present(this);
return (type: failure.unexpected, message: null);
}
2023-07-06 17:18:41 +03:30
}