56 lines
1.5 KiB
Dart
56 lines
1.5 KiB
Dart
import 'package:dio/dio.dart';
|
|
|
|
String describeLoadError(Object error) {
|
|
if (error is DioException) {
|
|
final statusCode = error.response?.statusCode;
|
|
|
|
if (statusCode == 401 || statusCode == 403) {
|
|
return 'Нет доступа: API key отклонён ($statusCode).';
|
|
}
|
|
|
|
if (_isNetworkFailure(error)) {
|
|
return 'Backend недоступен: ${error.message ?? error.type.name}.';
|
|
}
|
|
|
|
final responseMessage = _responseMessage(error);
|
|
if (responseMessage != null) {
|
|
return 'Backend вернул ошибку $statusCode: $responseMessage';
|
|
}
|
|
|
|
if (statusCode != null) {
|
|
return 'Backend вернул ошибку $statusCode.';
|
|
}
|
|
|
|
return 'Ошибка запроса: ${error.message ?? error.type.name}.';
|
|
}
|
|
|
|
if (error is FormatException) {
|
|
return 'Неожиданный ответ backend: ${error.message}';
|
|
}
|
|
|
|
return error.toString();
|
|
}
|
|
|
|
bool _isNetworkFailure(DioException error) {
|
|
return switch (error.type) {
|
|
DioExceptionType.connectionTimeout ||
|
|
DioExceptionType.sendTimeout ||
|
|
DioExceptionType.receiveTimeout ||
|
|
DioExceptionType.connectionError ||
|
|
DioExceptionType.unknown => true,
|
|
_ => false,
|
|
};
|
|
}
|
|
|
|
String? _responseMessage(DioException error) {
|
|
final data = error.response?.data;
|
|
if (data is Map) {
|
|
final value = data['detail'] ?? data['message'] ?? data['error'];
|
|
return value?.toString();
|
|
}
|
|
if (data is String && data.trim().isNotEmpty) {
|
|
return data.trim();
|
|
}
|
|
return null;
|
|
}
|