Files
jinzhi/lib/common/network/api_error_message.dart

25 lines
690 B
Dart

import 'package:dio/dio.dart';
import 'api_exception.dart';
String? kratosReason(Object e) {
if (e is DioException || e is ApiException || e is FormatException) {
return ApiException.from(e).reason;
}
return null;
}
String kratosDisplayMessage(Object e, {required String fallback}) {
if (kratosReason(e) == 'INTERNAL_ERROR') return fallback;
final exception = ApiException.from(e);
if (exception.serverMessage case final message? when message.isNotEmpty) {
return message;
}
return fallback;
}
abstract final class ErrorMessageMapper {
static String message(Object error, {required String fallback}) =>
kratosDisplayMessage(error, fallback: fallback);
}