Blame view

lib/common/request/exception.dart 3.71 KB
056970d8   Key   feat: api
1
  import 'package:dio/dio.dart';
05f9b20a   Key   fixed: api调用方式,未完善
2
3
  import 'package:flutter/foundation.dart';
  
056970d8   Key   feat: api
4
5
6
7
  import 'api_response/api_response_entity.dart';
  
  class ApiException implements Exception {
    static const unknownException = "未知错误";
1892df31   Key   优化接口调用
8
    static const customErrorCode = -1;
056970d8   Key   feat: api
9
10
11
12
13
14
    final String? message;
    final int? code;
    String? stackInfo;
  
    ApiException([this.code, this.message]);
  
1892df31   Key   优化接口调用
15
16
17
18
19
    factory ApiException.fromDioException(DioException exception) {
      if (kDebugMode) {
        print('fromDioException 异常: $exception');
      }
      switch (exception.type) {
056970d8   Key   feat: api
20
        case DioExceptionType.connectionTimeout:
1892df31   Key   优化接口调用
21
          return BadRequestException(customErrorCode, "连接超时");
056970d8   Key   feat: api
22
        case DioExceptionType.sendTimeout:
1892df31   Key   优化接口调用
23
          return BadRequestException(customErrorCode, "请求超时");
056970d8   Key   feat: api
24
        case DioExceptionType.receiveTimeout:
1892df31   Key   优化接口调用
25
          return BadRequestException(customErrorCode, "响应超时");
056970d8   Key   feat: api
26
        case DioExceptionType.badCertificate:
1892df31   Key   优化接口调用
27
          return BadRequestException(customErrorCode, "证书错误");
056970d8   Key   feat: api
28
        case DioExceptionType.badResponse:
1892df31   Key   优化接口调用
29
          return BadRequestException(customErrorCode, "返回错误");
056970d8   Key   feat: api
30
        case DioExceptionType.cancel:
1892df31   Key   优化接口调用
31
          return BadRequestException(customErrorCode, "请求取消");
056970d8   Key   feat: api
32
        case DioExceptionType.connectionError:
1892df31   Key   优化接口调用
33
          return BadRequestException(customErrorCode, "连接错误");
056970d8   Key   feat: api
34
        case DioExceptionType.unknown:
1892df31   Key   优化接口调用
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
          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:
056970d8   Key   feat: api
57
  
1892df31   Key   优化接口调用
58
59
60
61
62
63
64
65
66
67
68
              /// 试一下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');
                }
              }
056970d8   Key   feat: api
69
          }
056970d8   Key   feat: api
70
      }
1892df31   Key   优化接口调用
71
72
73
      var errorCode = exception.response?.statusCode ?? customErrorCode;
      var errorMsg = kDebugMode ? (exception.response?.statusMessage ?? exception.message) : unknownException;
      return ApiException(errorCode, errorMsg);
056970d8   Key   feat: api
74
75
76
77
    }
  
    factory ApiException.from(dynamic exception) {
      if (exception is DioException) {
1892df31   Key   优化接口调用
78
        return ApiException.fromDioException(exception);
056970d8   Key   feat: api
79
80
81
      }
      if (exception is ApiException) {
        return exception;
056970d8   Key   feat: api
82
      }
1892df31   Key   优化接口调用
83
84
85
      var apiException = ApiException(-1, unknownException);
      apiException.stackInfo = exception?.toString();
      return apiException;
056970d8   Key   feat: api
86
87
88
89
90
91
92
93
94
95
    }
  }
  
  /// 请求错误
  class BadRequestException extends ApiException {
    BadRequestException([int? code, String? message]) : super(code, message);
  }
  
  /// 未认证异常
  class UnauthorisedException extends ApiException {
1892df31   Key   优化接口调用
96
    UnauthorisedException([int? code, String? message]) : super(code, message);
056970d8   Key   feat: api
97
  }