如何在获取 API 中处理 HTTP 代码 4xx 响应

How to handle HTTP code 4xx responses in fetch api

本文关键字:HTTP 代码 4xx 响应 处理 获取 API      更新时间:2023-09-26

我想知道当我们使用 ajax 函数时,我们应该如何处理来自后端的 400。我们可以在承诺解析函数中做出 if 语句,并检查 res 状态是否为 400。不同的方法是为获取制作包装服务,当我们从服务器获得 400 时,我们会抛出异常。如何处理这个问题?

我建议使用一个包装器来检查response.ok如果响应代码为 2xx 则为真。

请注意 fetch() MDN 页面中的此声明:

对成功 fetch(( 的准确检查将包括检查承诺已解决,然后检查 Response.ok 属性是否具有值为 true。HTTP 状态 404 不构成网络错误。

这是一个这样的包装器:

function fetchData() {
    return fetch.apply(null, arguments).then(response => {
         if (!response.ok) {
             // create error object and reject if not a 2xx response code
             let err = new Error("HTTP status code: " + response.status)
             err.response = response
             err.status = response.status
             throw err
         }
         return response
    })
}

这样,我们可以相应地处理所有类型的状态。

fetch(url, {
  method: 'POST',
  headers: headers,
  body: JSON.stringify({ user_email: email }),
}).then((response) => {
  return new Promise((resolve) => response.json()
    .then((json) => resolve({
      status: response.status,
      ok: response.ok,
      json,
    })));
}).then(({ status, json, ok }) => {
  const message = json.message;
  let color = 'black';
  switch (status) {
    case 400:
      color = 'red';
      break;
    case 201:
    case 200:
      color = 'grey';
      break;
    case 500:
    default:
      handleUnexpected({ status, json, ok });
  }
})

灵感

将其合并到HTTP抽象中可能是个好主意。也许有某种options论点:

const myFetch = (method, path, {headers, strictErrors, whatever}) => {
  // fetch here, if strictErrors is true, reject on error.
  // return a Promise.
}
myFetch('GET', 'somepath', {strictErrors: true})
  .then(response => {})
  .catch(err => { /* maybe 400 */ });

围绕fetch的包装器通常是一个好主意,fetch是一个相对低级的函数。正如直接在任何地方创建新的 XHR 对象不是一个好主意一样,我认为在应用程序的各个部分直接调用 fetch() 也不是一个好主意。在某些方面,它类似于全局变量。

我为此找到的最好的方法是将其包装在一个新的承诺中,如果response.ok为假,则拒绝带有错误上下文的承诺。

/**
 * Parses the JSON returned by a network request
 *
 * @param  {object} response A response from a network request
 *
 * @return {object}          The parsed JSON, status from the response
 */
function parseJSON(response) {
  return new Promise((resolve) => response.json()
    .then((json) => resolve({
      status: response.status,
      ok: response.ok,
      json,
    })));
}
/**
 * Requests a URL, returning a promise
 *
 * @param  {string} url       The URL we want to request
 * @param  {object} [options] The options we want to pass to "fetch"
 *
 * @return {Promise}           The request promise
 */
export default function request(url, options) {
  return new Promise((resolve, reject) => {
    fetch(endpoint  + url, options)
      .then(parseJSON)
      .then((response) => {
        if (response.ok) {
          return resolve(response.json);
        }
        // extract the error from the server's json
        return reject(response.json.meta.error);
      })
      .catch((error) => reject({
        networkError: error.message,
      }));
  });
}

(https://github.com/github/fetch/issues/203 的热门评论(