Getting UnhandledPromiseRejection in Solid Start server function that stops dev server
I have an
async function in my Solid Start app and am receiving the following error message whenever it returns an Error.
Here is the function:
Not sure exactly what's going on and hoping someone can perhaps provide insight.23 Replies
I assume it's being used like this?
In any other context the
thrown redirect would be interpreted as an error.That is close to correct -- I have the
use server directive at the top of the file/module.So the
action is in another module that doesn't contain "use server" (which would be correct usage) and the action module simply imports loginOrRegister from the "use server" module?
FYI: Because of the confusion that top level "use server" causes, it is generally discouraged. "use server" marks an RPC boundary and both query and action need to be on the client side of that boundary; they won't work correctly on the server side of that boundary.Gotcha -- so here's the breakdown with two files/modules. This was all generated from the Create Solid CLI starter (with Drizzle option checked):
First one here is
./src/api/index.ts
and the second is ./src/api/server.ts
I believe the above aligns with what you describedSo far so good. So client-side code should only ever
rather than
I believe the latter would result in a bare RPC call without the
action wrapper that interprets thrown redirects.That's good to know. Yeah, it's being used client side in tthe following manner:
Just not sure why when I invoke the error path (e.g. submitting without username and password) while in dev mode with my dev server running, the failure path on the server side chokes with the unhandled exception and shuts down my dev server which is not a graceful way to handle that. Seems like the wrong behavior to me. Not sure if it's something that I'm doing wrong from a workflow perspective / component design.
Have you been able to get a minimal
logHello to work?
Sometimes other code interferes.
FYI: https://discord.com/channels/722131463138705510/1307001733905645599
but we covered that.
Another thought: You are redirecting to '/' ; is it possible there is a function in load for src/routes/index.tsxthat misbehaves once the login has been successful?Ah, that's funny -- I think that issue is the same exact problem I'm facing. In my case, the success path works fine without a problem. Haven't noticed any issues when logging in successfully. It only is the path where I return a
new Error() in the code path for logging in on the server.
Judging from the other post, could the problem be in me using getUser in some premature way? Below is my app's root layout
Does anything look unexpected here?Nothing jumps out at me. Perhaps open the network tab and look the
server_ requests.
The RPC responsible for the server croaking would likely not send a response. The request will have an X-Server-Id header with something like
That may give you a path to determine whether or not the loginOrRegister RPC is the actual culprit.
Then comment stuff out to see when it stops to break.This is what I see from the response from
server_
Given that there is a response, why do you suspect that it is this request that is giving the server problems (sorry if I'm being slow).
Just for comparison
This is the payload from similar code where an
Error is returned (rather than thrown) as a result of the server side function.
Though perhaps your p.status = "failure", means something went sideways.
Looking at this it looks like a route guard (redirect to '/login') is being triggered as well
… maybe something in there is responsible for the p.status = "failure"?
BTW: in your OP I assumed that the message was from the server side (node:internal and all that). Was I wrong and you actually got that from the client side console?I'm all new to this myself in using Solid Start for the first time -- apologies if I'm not explaining things correctly. Basically, when I comment out that line where I return the
Error object in my log in server function, the dev server doesn't croak and die. When I keep it and see it run, I actually see the error message appear on my UI that the Error returned, but the dev server dies once it appears. I took that as an indication that something perhaps went wrong on the server side and not client side 🤔
Yeah, this is all happening on my /login route page. I'm basically submitting an empty form and testing my failure/error path in my code.As you can see here:
https://github.com/solidjs-community/strello/blob/9c9ae973d96cc045914e696757a1b5f31efc6fa1/src/lib/index.ts#L22
returning an
Error instance shouldn't cause any problems.
There doesn't seem to be anything untoward with this part
It's the second part that is unexpected.
The fact that removing the Error fixes things is just weird. I suspect something else is going on.GitHub
strello/src/lib/index.ts at 9c9ae973d96cc045914e696757a1b5f31efc6fa...
Contribute to solidjs-community/strello development by creating an account on GitHub.
Is there anywhere in your code where you
redirect('/login')?I'm wondering whether this is problematic:
If you are throwing a
redirect('/login') when navigating to /login …
The typical pattern is to place a route guard on /login to redirect('/') for already authenticated visitors.
https://github.com/solidjs-community/strello/blob/9c9ae973d96cc045914e696757a1b5f31efc6fa1/src/routes/login.tsx#L11
https://github.com/solidjs-community/strello/blob/9c9ae973d96cc045914e696757a1b5f31efc6fa1/src/lib/index.ts#L41-L49
Any page requiring authentication uses preload:
https://github.com/solidjs-community/strello/blob/9c9ae973d96cc045914e696757a1b5f31efc6fa1/src/routes/index.tsx#L62
https://github.com/solidjs-community/strello/blob/9c9ae973d96cc045914e696757a1b5f31efc6fa1/src/lib/index.ts#L7-L14
to getUser() which will result in a redirect('/login') if there is no authentication.GitHub
strello/src/routes/index.tsx at 9c9ae973d96cc045914e696757a1b5f31ef...
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.
GitHub
strello/src/lib/index.ts at 9c9ae973d96cc045914e696757a1b5f31efc6fa...
Contribute to solidjs-community/strello development by creating an account on GitHub.
Then again, it seems to do the same thing here
https://github.com/solidjs-community/strello/blob/9c9ae973d96cc045914e696757a1b5f31efc6fa1/src/components/Layout.tsx#L7
GitHub
strello/src/components/Layout.tsx at 9c9ae973d96cc045914e696757a1b5...
Contribute to solidjs-community/strello development by creating an account on GitHub.
Yeah, it's an interesting problem...it's unfortunately blocking me from using Solid Start because of it. I may just pivot to doing a vanilla Solid SPA with an Elysia backend server if I'm not able to resolve the issue
Only downside to going vanilla like that is that I'm not able to make use of the Data APIs within
@solid/router if I'm not mistakenWhat you lose is SSR and RPC (
"use server"); if you use solid-router you can still use query and createAsync which will move to the core with 2.0.FYI: Here is an example of a SolidStart drizzle app:
https://github.com/stefan-karger/kreativ-hub
It had some teething issues
https://discord.com/channels/722131463138705510/722131463889223772/1304368665763774514
If you have a look right now it only uses the function scope form of
"use server".GitHub
GitHub - stefan-karger/kreativ-hub: A creative management platform ...
A creative management platform built with SolidJS. - stefan-karger/kreativ-hub
@peerreynders btw, do I also lose the ability to use
action from Solid Router in my Elysia + Solid SPA approach?actions are entirely client side. With SPA they just tend to be more verbose than with "use server" and of course single flight mutations can't be supported.Thanks for all your help and input in this thread. You've been incredibly invaluable.
Yeah, I figured that I would lose single flight mutations in this road. That's a bullet I'm gonna have to bite. I've been fighting Solid Start way too much over these past few days over authenticated routes and how to properly manage them.
Just my two cents, I feel that the documentation needs to be improved in perhaps adding examples around this use-case of properly handling protected routes within Solid Start.
adding examples around this use-case of properly handling protected routes within Solid Start.For that I'm mostly guided by how strello implements it. -
getUser at the top level layout which throws a redirect('/login') if there is no active authentication.
- while redirectIfLoggedIn on the /login route preload throws a redirect('/') if there is active authentication.
This builds on the approach used in the with-auth example.GitHub
GitHub - solidjs-community/strello
Contribute to solidjs-community/strello development by creating an account on GitHub.
GitHub
solid-start/examples/with-auth at main · solidjs/solid-start
SolidStart, the Solid app framework. Contribute to solidjs/solid-start development by creating an account on GitHub.
GitHub
strello/src/components/Layout.tsx at 9c9ae973d96cc045914e696757a1b5...
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.