HttpError.dioError constructor

HttpError.dioError(
  1. DioError error
)

处理dio报错

Implementation

HttpError.dioError(DioError error) {
  this.cause = error;
  switch (error.type) {
    case DioErrorType.connectTimeout:
    case DioErrorType.receiveTimeout:
    case DioErrorType.sendTimeout:
      httpErrorType = HttpErrorType.TimeOutError;
      logMsg = '请求超时';
      message = HTTP_TIMEOUT_MSG;
      break;
    case DioErrorType.response:
      httpErrorType = HttpErrorType.ResponseError;
      message = HTTP_ERROR_MSG;
      try {
        int errCode = error.response.statusCode;
        switch (errCode) {
          case 400:
            logMsg = '400请求语法错误';
            break;
          case 401:
            logMsg = '401没有权限';
            break;
          case 403:
            logMsg = '403服务器拒绝执行';
            break;
          case 404:
            logMsg = '404无法连接服务器';
            break;
          case 405:
            logMsg = '405请求方法被禁止';
            break;
          case 500:
            logMsg = '500服务器内部错误';
            break;
          case 502:
            logMsg = '502无效的请求';
            break;
          case 503:
            logMsg = '503服务器挂了';
            break;
          case 505:
            logMsg = '505不支持HTTP协议请求';
            break;
          default:
            logMsg = error.response.statusMessage;
        }
      } on Exception catch (_) {
        logMsg = '未知错误';
      }
      break;
    case DioErrorType.cancel:
      httpErrorType = HttpErrorType.CancelError;
      logMsg = CANCEL_MSG;
      break;
    case DioErrorType.other:
      httpErrorType = HttpErrorType.Unknown;
      message = UNKNOWN_MSG + ': ' + error.error.toString();
      break;
  }
}