2.2.2 • Published 5 years ago

@ezpnetwork/util-better-request v2.2.2

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

安装

yarn add @ezpnetwork/util-better-request

使用

import request, {
  NetworkError,
  ClientError,
  UnauthorizedError,
  ForbiddenError,
  ServerError,
} from '@jcnetwork/util-better-request';

async function doRequest() {
  try {
    const responsePayload = await request({
      url: '',
      method: '',

    });

    // 使用响应体
  } catch (err) {
    if (err instanceof NetworkError) {
      // 网络异常或超时,根本没有从服务器获取到任何响应的情况
    } else if (err instanceof ServerError) {
      // 服务器错误 status >= 500 && status < 600的情况
    } else if (err instanceof UnauthorizedError) {
      // 客户端未认证,通常表示此次请求没有附加认证信息或认证信息已经过期, status === 401的情况
    } else if (err instanceof ForbiddenError) {
      // 客户端没有访问权限,表示此次请求附加的认证信息已通过认证,但是此客户没有访问此资源的权限, status === 403的情况
    } else if (err instanceof ClientError) {
      // 客户端错误,表示客户发出的请求不满足API文档中定义的要求, status >= 400 && status < 500 && status !== 401 && status !== 403的情况                  
    }
  }
}

doRequest();