exception.dart
3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'api_response/api_response_entity.dart';
class ApiException implements Exception {
static const unknownException = "未知错误";
static const customErrorCode = -1;
final String? message;
final int? code;
String? stackInfo;
ApiException([this.code, this.message]);
factory ApiException.fromDioException(DioException exception) {
if (kDebugMode) {
print('fromDioException 异常: $exception');
}
switch (exception.type) {
case DioExceptionType.connectionTimeout:
return BadRequestException(customErrorCode, "连接超时");
case DioExceptionType.sendTimeout:
return BadRequestException(customErrorCode, "请求超时");
case DioExceptionType.receiveTimeout:
return BadRequestException(customErrorCode, "响应超时");
case DioExceptionType.badCertificate:
return BadRequestException(customErrorCode, "证书错误");
case DioExceptionType.badResponse:
return BadRequestException(customErrorCode, "返回错误");
case DioExceptionType.cancel:
return BadRequestException(customErrorCode, "请求取消");
case DioExceptionType.connectionError:
return BadRequestException(customErrorCode, "连接错误");
case DioExceptionType.unknown:
int? errCode = exception.response?.statusCode;
switch (errCode) {
case 400:
return BadRequestException(errCode, "请求语法错误");
case 401:
return UnauthorisedException(errCode, "没有权限");
case 403:
return UnauthorisedException(errCode, "服务器拒绝执行");
case 404:
return UnauthorisedException(errCode, "无法连接服务器");
case 405:
// 会出发登出的code,这里强制换掉
return UnauthorisedException(406, "请求方法被禁止");
case 500:
return UnauthorisedException(errCode, "服务器内部错误");
case 502:
return UnauthorisedException(errCode, "无效的请求");
case 503:
return UnauthorisedException(errCode, "服务器异常");
case 505:
return UnauthorisedException(errCode, "不支持HTTP协议请求");
default:
/// 试一下Response能不能解析出业务上的错误码,不能再继续走dio抛出的异常
try {
ApiResponse apiResponse = ApiResponse.fromJson(exception.response?.data);
if (apiResponse.code != null) {
return ApiException(apiResponse.code, apiResponse.msg ?? "错误码:${apiResponse.code}");
}
} catch (e) {
if (kDebugMode) {
print('DioExceptionType.unknown 错误: $e');
}
}
}
}
var errorCode = exception.response?.statusCode ?? customErrorCode;
var errorMsg = kDebugMode ? (exception.response?.statusMessage ?? exception.message) : unknownException;
return ApiException(errorCode, errorMsg);
}
factory ApiException.from(dynamic exception) {
if (exception is DioException) {
return ApiException.fromDioException(exception);
}
if (exception is ApiException) {
return exception;
}
var apiException = ApiException(-1, unknownException);
apiException.stackInfo = exception?.toString();
return apiException;
}
@override
String toString() {
return 'ApiException{code: $code, message: $message, stackInfo: $stackInfo}';
}
}
/// 请求错误
class BadRequestException extends ApiException {
BadRequestException([int? code, String? message]) : super(code, message);
}
/// 未认证异常
class UnauthorisedException extends ApiException {
UnauthorisedException([int? code, String? message]) : super(code, message);
}