youhua
This commit is contained in:
38
flutter_monisuo/lib/core/network/api_exception.dart
Normal file
38
flutter_monisuo/lib/core/network/api_exception.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
/// API 异常类
|
||||
class ApiException implements Exception {
|
||||
final String message;
|
||||
final String code;
|
||||
final int? statusCode;
|
||||
|
||||
ApiException({
|
||||
required this.message,
|
||||
required this.code,
|
||||
this.statusCode,
|
||||
});
|
||||
|
||||
factory ApiException.unauthorized([String? message]) {
|
||||
return ApiException(
|
||||
message: message ?? '未授权',
|
||||
code: '0002',
|
||||
statusCode: 401,
|
||||
);
|
||||
}
|
||||
|
||||
factory ApiException.networkError([String? message]) {
|
||||
return ApiException(
|
||||
message: message ?? '网络错误',
|
||||
code: 'NETWORK_ERROR',
|
||||
);
|
||||
}
|
||||
|
||||
factory ApiException.serverError([String? message]) {
|
||||
return ApiException(
|
||||
message: message ?? '服务器错误',
|
||||
code: 'SERVER_ERROR',
|
||||
statusCode: 500,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => 'ApiException: $message (code: $code)';
|
||||
}
|
||||
@@ -53,7 +53,7 @@ class ApiResponse<T> {
|
||||
if (fromJsonT != null && data != null) {
|
||||
return ApiResponse.success(fromJsonT(data), msg);
|
||||
}
|
||||
return ApiResponse.success(data as T?, msg);
|
||||
return ApiResponse.success(data as T, msg);
|
||||
} else if (code == '0002') {
|
||||
return ApiResponse.unauthorized(msg);
|
||||
} else {
|
||||
|
||||
@@ -71,7 +71,7 @@ class AppTheme {
|
||||
color: AppColors.border,
|
||||
thickness: 1,
|
||||
),
|
||||
cardTheme: CardTheme(
|
||||
cardTheme: CardThemeData(
|
||||
color: AppColors.cardBackground,
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
|
||||
Reference in New Issue
Block a user