Best Practices for Handling Errors in a query-Wrapped Server Function
Hey everyone! I’ve been working with SolidStart and have a question about error handling and display strategies for server functions wrapped in a query. The example below is inspired by one of the official SolidStart examples:
In this example, if the user isn’t found, the current approach redirects to the login page. However, for a different scenario, I’d like to display an error message in the UI instead of redirecting.
I’m considering a couple of approaches:
1. Relying on Error Boundaries to catch and render the error.
2. Returning a value (e.g., { data: ..., error: ... }) that includes potential errors so I can display the appropriate state in the receiving component.
What do you think is the best way to handle this in SolidStart? I’d love to hear your thoughts and how you approach similar cases in your projects.
Thanks in advance.
6 Replies
TL;DR: Return
Error
s; throw redirect
s
I assume this comes from the with-auth example.
To be honest that version doesn't make a lot of sense to me as the "User not found " Error
would simply get swallowed in favour of the thrown redirect.
Instead have a look at how the strello
app handles it with a more distributed approach:
Ryan Carniato
YouTube
SolidStart: The Shape of Frameworks to Come
Join me to take a look at what SolidStart is shaping up to be. I will be going through a full tour of the framework to show you what it is, what you can do with it, and how it changes things.
[0:00:00] Intro
[0:05:30] The Shape of Frameworks to Come
[0:18:00] A Classic Client-Side Solid App
[0:30:45] Simple Todo App with a Router & a Form
[0:45...
GitHub
strello/src/lib/index.ts at 9c9ae973d96cc045914e696757a1b5f31efc6fa...
Contribute to solidjs-community/strello development by creating an account on GitHub.
GitHub
solid-start/examples/with-auth/src/lib/index.ts at 5812ac69632d0d3f...
SolidStart, the Solid app framework. Contribute to solidjs/solid-start development by creating an account on GitHub.
There are only two outcomes, either:
- resolve to the user info
- reject with a
redirect
compelling the client side router to navigate to /login
(or simply stay there)
getUser()
is used on the lop level Layout
:
This accomplishes two things:
- it makes the user
accessor available to show the correct "Login" vs. "Logout"
- but more importantly it always throws a redirect('/login')
whenever there isn't an active account ID in the session cookie. So you either stay on the /login
page or you will get navigated to the /login
page.GitHub
strello/src/components/Layout.tsx at 9c9ae973d96cc045914e696757a1b5...
Contribute to solidjs-community/strello development by creating an account on GitHub.
Now there is a counterpart:
This is part of the login route
preload
:
This ensures that you are automatically navigated from /login
to /
if there is an authenticated session.GitHub
strello/src/lib/index.ts at 9c9ae973d96cc045914e696757a1b5f31efc6fa...
Contribute to solidjs-community/strello development by creating an account on GitHub.
GitHub
strello/src/routes/login.tsx at 9c9ae973d96cc045914e696757a1b5f31ef...
Contribute to solidjs-community/strello development by creating an account on GitHub.
Finally the
login.tsx
route uses loginOrRegister
:
Again there are only two outcomes:
- resolve to an Error
instance
- reject with a redirect
compelling the client side router to navigate to /
The important feature is that the
Error
is returned, not thrown.
This means that in the login.tsx
route we can use the latest submission;
to obtain the most recent error from the submission record to display the error:
So either the login succeeds and you are navigated to /
or the Error
message is simply displayed in the current /login
page.GitHub
strello/src/routes/login.tsx at 9c9ae973d96cc045914e696757a1b5f31ef...
Contribute to solidjs-community/strello development by creating an account on GitHub.
Rely on thrown errors for unrecoverable problemshttps://discord.com/channels/722131463138705510/1274312740378251287/1274319949585973309