handling (http) errors with createResource
I don't understand how to get hold of the error when using the tutorial examples of fetching REST data. The error is always either empty, or not callable to me ("solid-js": "^1.8.11").
If I take the example from the documentation here and make it work. I shows the fetched data OK, but doesn't handle http404's. OK, I modify the
fetchUser
function to throw on non-ok:
the rendering gets broken because the user.error()
isn't a function š¤
When I replace user.error()
with the typeOf
and JSON.stringify
, they show an empty object {}
.
ā What do I do wrong, and how to get the resource errors reported properly?
I tried Promised.reject(new Error(...))
and axios.get
with no change in behavior.
Playground example: https://playground.solidjs.com/anonymous/3b5dbdc3-c242-4d24-8468-f7b5c98509edSolid Playground
Quickly discover what the solid compiler will generate from your JSX template
7 Replies
Values thrown in resources get caught by
ErrorBoundary
components above where the resource is readHey @Brendonovich , thank you for the quick reaction. Yes, an
ErrorBoundary
around the App
component does indeed catch the error thrown (if I remove the match on user.error
). Could make it work for some cases, but looks like a hack to me, since (AFAIU) the ErrorBoundaries are supposed to prevent whole applications crash from unexpected errors, not to handle the expected ones. I would like to have a more fine-grained control over the error handling, e.g. show an error saying "User doesn't exist" under the userId input - not replace the whole component with the wrapping fallback of the ErrorBoundary. Which, AFAIU, the examples in the documentation are trying to show me how to do, but something is amiss.You can put an error boundary around the Show and Match as well, but from what I understand in this case you're receiving specific errors that you want to display. I'd recommend not throwing these, instead return them as data. Rely on thrown errors for unrecoverable problems
OK, so what you're saying you wouldn't be relying on the
error
of the createResource
. ā
Yeah, send everything that you want to display specific UI for through
data
That also has the added benefit of being typesafe, so you can type narrow in Switch/Match
OK, so at the end it was a bug in the documentation, where they used
user.error()
instead of the user.error
for const user = createResource(...)
. Submitting a PR with a fix suggestion: https://github.com/solidjs/solid-docs-next/pull/847ah yea those are wrong