globalbrain/sefirot

[Http] Add helper function to check if the error is `FetchError`

Closed this issue · 2 comments

When dealing with errors, it would be nice to have a function to assert the error is FetchError so that we can access underlining properties.

import { isFetchError } from 'sefirot/Http/Http'

const m = useMutation(() => ...)

try {
  m.execute()
} catch (e) {
  if (isFetchError(e)) {
    if (e.data === 'Email already exists') {
      // ...
    }
  }
}

And, maybe we could have more powerful API too? Not sure.

try {
  m.execute()
} catch (e) {
  // Throw `e` if the error is not FetchError.
  // Else call closure.
  handleFetchErrorOrThrow(e, (e) => {
    // ...
  })
}

Already added isFetchError in #539 🫠 should we export it form Api or Http?

Already added isFetchError in #539 🫠

That's my man 😄

should we export it form Api or Http?

Good point. Wasn't thinking about that. Hmmm I think it should be from Http...? Because the error is coming from Http and not from API 🤔 ...