Blame view

lib/common/request/exception.dart 3.22 KB
056970d8   Key   feat: api
1
  import 'package:dio/dio.dart';
056970d8   Key   feat: api
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
  import 'api_response/api_response_entity.dart';
  
  class ApiException implements Exception {
    static const unknownException = "未知错误";
    final String? message;
    final int? code;
    String? stackInfo;
  
    ApiException([this.code, this.message]);
  
    factory ApiException.fromDioError(DioException error) {
      switch (error.type) {
        case DioExceptionType.connectionTimeout:
          return BadRequestException(-1, "连接超时");
        case DioExceptionType.sendTimeout:
          return BadRequestException(-1, "请求超时");
        case DioExceptionType.receiveTimeout:
          return BadRequestException(-1, "响应超时");
        case DioExceptionType.badCertificate:
          return BadRequestException(-1, "证书错误");
        case DioExceptionType.badResponse:
          return BadRequestException(-1, "返回错误");
        case DioExceptionType.cancel:
          return BadRequestException(-1, "请求取消");
        case DioExceptionType.connectionError:
          return BadRequestException(-1, "连接错误");
        case DioExceptionType.unknown:
          try {
            /// http错误码带业务错误信息
            ApiResponse apiResponse = ApiResponse.fromJson(error.response?.data);
            if (apiResponse.code != null) {
              return ApiException(apiResponse.code, apiResponse.msg);
            }
  
            int? errCode = error.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:
                return UnauthorisedException(errCode!, "请求方法被禁止");
              case 500:
                return UnauthorisedException(errCode!, "服务器内部错误");
              case 502:
                return UnauthorisedException(errCode!, "无效的请求");
              case 503:
                return UnauthorisedException(errCode!, "服务器异常");
              case 505:
                return UnauthorisedException(errCode!, "不支持HTTP协议请求");
              default:
                return ApiException(errCode, error.response?.statusMessage ?? '未知错误');
            }
          } on Exception catch (e) {
            return ApiException(-1, unknownException);
          }
        default:
          return ApiException(-1, error.message);
      }
    }
  
    factory ApiException.from(dynamic exception) {
      if (exception is DioException) {
        return ApiException.fromDioError(exception);
      }
      if (exception is ApiException) {
        return exception;
      } else {
        var apiException = ApiException(-1, unknownException);
        apiException.stackInfo = exception?.toString();
        return apiException;
      }
    }
  }
  
  /// 请求错误
  class BadRequestException extends ApiException {
    BadRequestException([int? code, String? message]) : super(code, message);
  }
  
  /// 未认证异常
  class UnauthorisedException extends ApiException {
    UnauthorisedException([int code = -1, String message = '']) : super(code, message);
  }