BaseException.fromDioError constructor

BaseException.fromDioError(
  1. DioException error
)

Implementation

factory BaseException.fromDioError(DioException error) {
  switch (error.type) {
    case DioExceptionType.cancel:
      return BaseException(-1, "请求取消",ResponseErrorType.cancel);
    case DioExceptionType.connectionTimeout:
      return BaseException(-1, "连接超时",ResponseErrorType.connectionTimeout);
    case DioExceptionType.sendTimeout:
      return BaseException(-1, "请求超时",ResponseErrorType.sendTimeout);
    case DioExceptionType.receiveTimeout:
      return BaseException(-1, "响应超时",ResponseErrorType.receiveTimeout);
    case DioExceptionType.badResponse:
      try {
        /// http错误码带业务错误信息
        BaseException apiResponse = BaseException.fromJson(error.response?.data);
        if (apiResponse.code != null) {
          return BaseException(apiResponse.code, apiResponse.message,ResponseErrorType.businessError);
        }

        int? errCode = error.response?.statusCode;
        switch (errCode) {
          case 400:
            return BaseException(errCode, "请求语法错误",ResponseErrorType.error400);
          case 401:
            return BaseException(errCode!, "没有权限",ResponseErrorType.error401);
          case 403:
            return BaseException(errCode!, "服务器拒绝执行",ResponseErrorType.error403);
          case 404:
            return BaseException(errCode!, "无法连接服务器",ResponseErrorType.error404);
          case 405:
            return BaseException(errCode!, "请求方法被禁止",ResponseErrorType.error405);
          case 500:
            return BaseException(errCode!, "服务器内部错误",ResponseErrorType.error500);
          case 502:
            return BaseException(errCode!, "无效的请求",ResponseErrorType.error502);
          case 503:
            return BaseException(errCode!, "服务器异常",ResponseErrorType.error503);
          case 505:
            return BaseException(errCode!, "不支持HTTP协议请求",ResponseErrorType.error505);
          default:
            return BaseException(
                errCode, error.response?.statusMessage ?? '未知错误',error.type==DioExceptionType.connectionError?ResponseErrorType.noNetwork:ResponseErrorType.unknowError);
        }
      } on Exception {
        return BaseException(-1, unknownException,error.type==DioExceptionType.connectionError?ResponseErrorType.noNetwork:ResponseErrorType.unknowError);
      }
    default:
      return BaseException(-1, error.message,error.type==DioExceptionType.connectionError?ResponseErrorType.noNetwork:ResponseErrorType.unknowError);
  }
}