createErrorEntity function

ErrorEntity createErrorEntity(
  1. DioError error
)

Implementation

ErrorEntity createErrorEntity(DioError error) {
  switch (error.type) {
    case DioErrorType.cancel:
      {
        return ErrorEntity(-1, "请求取消");
      }
    case DioErrorType.connectTimeout:
      {
        return ErrorEntity(-1, "连接超时");
      }

    case DioErrorType.sendTimeout:
      {
        return ErrorEntity(-1, "请求超时");
      }

    case DioErrorType.receiveTimeout:
      {
        return ErrorEntity(-1, "响应超时");
      }

    case DioErrorType.response:
      {
        try {
          var errCode = error.response?.statusCode;
          switch (errCode) {
            case 400:
              return ErrorEntity(errCode, "请求语法错误");
            case 403:
              return ErrorEntity(errCode, "服务器拒绝执行");
            case 404:
              return ErrorEntity(errCode, "无法连接服务器");
            case 405:
              return ErrorEntity(errCode, "请求方法被禁止");
            case 500:
              return ErrorEntity(errCode, "服务器内部错误");
            case 502:
              return ErrorEntity(errCode, "无效的请求");
            case 503:
              return ErrorEntity(errCode, "服务器挂了");

            case 505:
              return ErrorEntity(errCode, "不支持HTTP协议请求");
            default:
              {
                return ErrorEntity(errCode, "未知错误");
              }
          }
        } on Exception catch (_) {
          return ErrorEntity(-1, "未知错误");
        }
      }

    default:
      {
        return ErrorEntity(-1, error.message);
      }
  }
}