feat: implement core architecture, design system, localization, and trip features for both driver and rider apps.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
/// استثناءات الشبكة الموحّدة — كل خطأ يتحول إلى صنف هنا.
|
||||
/// docs/23 §4
|
||||
class ApiException implements Exception {
|
||||
ApiException(this.message, {this.statusCode, this.type});
|
||||
|
||||
final String message;
|
||||
final int? statusCode;
|
||||
final ApiExceptionType? type;
|
||||
|
||||
@override
|
||||
String toString() => 'ApiException($statusCode): $message';
|
||||
}
|
||||
|
||||
enum ApiExceptionType {
|
||||
network,
|
||||
timeout,
|
||||
unauthorized,
|
||||
forbidden,
|
||||
notFound,
|
||||
server,
|
||||
unknown,
|
||||
}
|
||||
|
||||
/// تحليل DioException إلى ApiException
|
||||
ApiException fromDioError(Object error) {
|
||||
final dio = error;
|
||||
final type = switch (dio.runtimeType.toString()) {
|
||||
'DioException' => _mapDioType(dio),
|
||||
_ => ApiExceptionType.unknown,
|
||||
};
|
||||
|
||||
final statusCode = _extractStatusCode(dio);
|
||||
final message = _extractMessage(dio, statusCode);
|
||||
|
||||
return ApiException(message, statusCode: statusCode, type: type);
|
||||
}
|
||||
|
||||
int? _extractStatusCode(Object error) {
|
||||
try {
|
||||
return (error as dynamic).response?.statusCode as int?;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
String _extractMessage(Object error, int? statusCode) {
|
||||
try {
|
||||
final data = (error as dynamic).response?.data;
|
||||
if (data is Map) {
|
||||
return (data['message'] ?? data['error'] ?? 'خطأ غير معروف').toString();
|
||||
}
|
||||
} catch (_) {}
|
||||
return switch (statusCode) {
|
||||
401 => 'انتهت الجلسة',
|
||||
403 => 'غير مصرح',
|
||||
404 => 'غير موجود',
|
||||
500 => 'خطأ في السيرفر',
|
||||
_ => 'خطأ غير معروف',
|
||||
};
|
||||
}
|
||||
|
||||
ApiExceptionType _mapDioType(Object error) {
|
||||
try {
|
||||
final dioType = (error as dynamic).type;
|
||||
return switch (dioType.toString()) {
|
||||
'DioExceptionType.connectionTimeout' => ApiExceptionType.timeout,
|
||||
'DioExceptionType.sendTimeout' => ApiExceptionType.timeout,
|
||||
'DioExceptionType.receiveTimeout' => ApiExceptionType.timeout,
|
||||
'DioExceptionType.connectionError' => ApiExceptionType.network,
|
||||
'DioExceptionType.badResponse' => ApiExceptionType.server,
|
||||
'DioExceptionType.cancel' => ApiExceptionType.unknown,
|
||||
_ => ApiExceptionType.unknown,
|
||||
};
|
||||
} catch (_) {
|
||||
return ApiExceptionType.unknown;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user