elbywan / wretch

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

Introduce isWretchError property inside WretchError type #187

Closed ssijak closed 1 year ago

ssijak commented 1 year ago

Self explanatory, it would allow us to easily distinguish if an error was wretch error or something else, e.g.:

try {
  await somethingThatCanThrow();
  await somethingElseThatCanThrow();
  await somethingThatCanThrowButUsesWretch();
} catch (err) {
  if((err as any).isWretchError) {
     //do something
  }
}
elbywan commented 1 year ago

Hey @ssijak,

I do not think that adding a new property is necessary, you can check if the error is an instance of WretchError already:

import wretch from 'wretch'

const todo = await wretch("https://jsonplaceholder.typicode.com/todos/BAD_ID")
  .get()
  .json()
  .catch(error => console.log("WretchError?", error instanceof wretch.WretchError))

// prints: WretchError? true

Related: https://github.com/elbywan/wretch/issues/79