npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hcfy/fetch-error

v1.1.0

Published

## 动机

Downloads

3

Readme

FetchError

动机

在使用 Fetch API 时,错误处理有点棘手:

  • 服务器返回非 200 的 HTTP 响应码时,fetch 不会抛错,需要我们自己手动生成一个错误对象
  • 出现网络错误时,fetch 抛出的错误没有特征,无法准确分辨一个错误是 fetch 抛出的还是其它代码抛出的

为此,这个模块定义了统一的 FetchError。

如何使用

import FetchError, { isFetchError } from '@hcfy/fetch-error'

function myFetch() {
  const request = { url: '...', method: 'POST' }
  return fetch(request.url, request).then(
    (response) => {
      if (!response.ok) {
        throw new FetchError(response, request)
      }
      return response
    },
    (e) => {
      return Promise.reject(new FetchError(e, request))
    }
  )
}

myFetch().then(
  (response) => {
    // 代码执行到这里,response.ok 一定是 true
  },
  (error) => {
    if (isFetchError(error)) {
      // 这是 fetch 抛出的错
      // 可以访问 request
      console.log(error.request)

      if (error.response) {
        // 这是由于 response.ok 不是 true 而抛的错
      } else {
        // 这是 fetch 直接抛的错,即网络问题导致请求没有送达服务器、或者被用户中断了。
        // 可以通过 error.cause 拿到具体错误。
        const originError = error.cause
      }
    } else {
      // 这是其它错误
    }
  }
)

注意事项

FetchError 依赖全局环境中的 Response 来判断第一个参数是 response 还是引起报错的 cause,所以:

  1. 你需要确保全局环境中有 Response
  2. 你需要确保你的 fetch 函数返回的 response 是全局环境中的 Response 的实例

举个例子,Node.js v18 中在全局环境中已有 Response,但如果你是用 node-fetch 发起的请求,而由于 node-fetch 的 response 并不是 Node.js v18 中 Response 的实例,这就会导致 FetchError 将它识别为错误 cause。这种情况下,你需要将全局环境中的 Response 修改为 node-fetch 的 Response。