error_handle.dart
1.88 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
/**
* error_handle.dart
*
* Created by iotjin on 2020/07/08.
* description: 异常处理
*/
import 'dart:io';
import 'package:dio/dio.dart';
class ExceptionHandle {
static const int success = 200;
static const int success_not_content = 204;
static const int unauthorized = 401;
static const int forbidden = 403;
static const int not_found = 404;
static const int net_error = 1000;
static const int parse_error = 1001;
static const int socket_error = 1002;
static const int http_error = 1003;
static const int timeout_error = 1004;
static const int cancel_error = 1005;
static const int unknown_error = 9999;
static NetError handleException(DioError error) {
if (error is DioError) {
if (error.type == DioErrorType.DEFAULT ||
error.type == DioErrorType.RESPONSE) {
dynamic e = error.error;
if (e is SocketException) {
return NetError(socket_error, '网络异常,请检查你的网络!');
}
if (e is HttpException) {
return NetError(http_error, '服务器异常!');
}
if (e is FormatException) {
return NetError(parse_error, '数据解析错误!');
}
return NetError(net_error, '网络异常,请检查你的网络!');
} else if (error.type == DioErrorType.CONNECT_TIMEOUT ||
error.type == DioErrorType.SEND_TIMEOUT ||
error.type == DioErrorType.RECEIVE_TIMEOUT) {
// 连接超时 || 请求超时 || 响应超时
return NetError(timeout_error, '连接超时!');
} else if (error.type == DioErrorType.CANCEL) {
return NetError(cancel_error, '取消请求');
} else {
return NetError(unknown_error, '未知异常');
}
} else {
return NetError(unknown_error, '未知异常');
}
}
}
class NetError {
int code;
String msg;
NetError(this.code, this.msg);
}