elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.83k stars 98 forks source link

Determining fetch error type #164

Closed qwe3qwe3lott closed 1 year ago

qwe3qwe3lott commented 1 year ago

Good day!

I am using .fetchError chain to catch fetch errors. Into .fetchError`s callback I need to execute different functions based on different errors.

For example: (failed)net::ERR_INTERNET_DISCONNECTED => execute some fucntion CORS error => execute other function

But into .fetchError chain errors above return error with the same text - "TypeError: Failed to fetch"

Is there some way to determining such cases?

wretch version: 2.1.5

elbywan commented 1 year ago

Hey @qwe3qwe3lott 👋 ,

Is there some way to determining such cases?

This is not really related to wretch, and I'm not sure there is an easy way of doing this with the information contained in the error object itself.

The spec states that fetch can throw TypeError in a variety of cases and I guess that the data is mostly implementation dependendent.

Refs:

qwe3qwe3lott commented 1 year ago

I'm not sure there is an easy way of doing this with the information contained in the error object itself

Unforturanly, it seems so(

Error object doesn`t contain reasons or details of error, so it is impossible to determine different cases.

Anyway, thanks for help!