globalbrain / sefirot

Global Brain Design System.
https://sefirot.globalbrains.com
MIT License
151 stars 12 forks source link

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

Open kiaking opened 2 weeks ago

kiaking commented 2 weeks ago

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) => {
    // ...
  })
}
brc-dd commented 2 weeks ago

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

kiaking commented 2 weeks ago

Already added isFetchError in https://github.com/globalbrain/sefirot/pull/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 🤔 ...