AppException.create constructor

AppException.create(
  1. DioException error
)

Implementation

factory AppException.create(DioException error) {
  switch (error.type) {
    case DioExceptionType.cancel: //https://github.com/cfug/dio/blob/main/dio/lib/src/dio_exception.dart
    // return BadRequestException(CANCEL_ERROR, "请求取消");
      return BadRequestException(CANCEL_ERROR, VFTextDelegateHelper.textDelegate.n_request_cancel);
    case DioExceptionType.connectionTimeout:
    // return BadRequestException(TIMEOUT_ERROR, "连接超时");
      return BadRequestException(TIMEOUT_ERROR, VFTextDelegateHelper.textDelegate.n_link_timeout);
    case DioExceptionType.sendTimeout:
    // return BadRequestException(TIMEOUT_ERROR, "请求超时");
      return BadRequestException(TIMEOUT_ERROR, VFTextDelegateHelper.textDelegate.n_request_timeout);
    case DioExceptionType.receiveTimeout:
    // return BadRequestException(TIMEOUT_ERROR, "响应超时");
      return BadRequestException(TIMEOUT_ERROR, VFTextDelegateHelper.textDelegate.n_reponse_timeout);
    case DioExceptionType.unknown:
      return AppException(unknown_error, VFTextDelegateHelper.textDelegate.n_unknown_error);
    default:
    // try {
      int? errCode = error.response?.statusCode;
      // String errMsg = error.response.statusMessage;
      // return ErrorEntity(code: errCode, message: errMsg);
      switch (errCode) {
        case 400:
        // return BadRequestException(errCode!, "请求语法错误");
          return BadRequestException(errCode!, VFTextDelegateHelper.textDelegate.n_400);
      // case 401:
      // // return UnauthorisedException(errCode!, "没有权限");
      //   return UnauthorisedException(errCode!, VFTextDelegateHelper.textDelegate.n_401);
        case 403:
        // return UnauthorisedException(errCode!, "服务器拒绝执行");
          return UnauthorisedException(errCode!, VFTextDelegateHelper.textDelegate.n_403);
        case 404:
        // return UnauthorisedException(errCode!, "无法连接服务器");
          return UnauthorisedException(errCode!, VFTextDelegateHelper.textDelegate.n_404);
        case 405:
        // return UnauthorisedException(errCode!, "请求方法被禁止");
          return UnauthorisedException(errCode!, VFTextDelegateHelper.textDelegate.n_405);
        case 500:
        // return UnauthorisedException(errCode!, "服务器内部错误");
          return UnauthorisedException(errCode!, VFTextDelegateHelper.textDelegate.n_500);
        case 502:
        // return UnauthorisedException(errCode!, "无效的请求");
          return UnauthorisedException(errCode!, VFTextDelegateHelper.textDelegate.n_502);
        case 503:
        // return UnauthorisedException(errCode!, "服务器挂了");
          return UnauthorisedException(errCode!, VFTextDelegateHelper.textDelegate.n_503);
        case 505:
        // return UnauthorisedException(errCode!, "不支持HTTP协议请求");
          return UnauthorisedException(errCode!, VFTextDelegateHelper.textDelegate.n_505);
        default:
          ReponseConfig co = ReponseConfigUtils.getResponseConfig()!;
          dynamic data = error.response?.data;

          dynamic code = data?[co.responseCodeName];
          dynamic message = data?[co.responseMessageName];
          dynamic responseData = data?[co.responseData];

          if (code!=null &&code is int ) {
            return AppException(code, message,responseData:responseData??"");
          }else{
            // return ErrorEntity(code: errCode, message: "未知错误");
            return AppException(unknown_error, VFTextDelegateHelper.textDelegate.n_unknown_error);
          }


      }
  // } on Exception catch (_) {
  //   // return AppException(unknown_error, "未知错误");
  //   return AppException(unknown_error, VFTextDelegateHelper.textDelegate.n_unknown_error);
  }
  // default:
  //   {
  //     return AppException(unknown_error, error.error.message);
  //   }
}