I get ERR_UNHANDLED_REJECTION

Hey all, I am using the solidstart drizzle example wiht login. solidstartversion: "@solidjs/start": "1.0.6", nodeversion 23.2 I try to run this code.
export async function loginOrRegister(formData: FormData) {
const username = String(formData.get("username"));
const password = String(formData.get("password"));
const loginType = String(formData.get("loginType"));

let error = validateUsername(username) || validatePassword(password);
if (error) return new Error(error);

try {
const user = await (loginType !== "login" ? register(username, password) : login(username, password));

createRedmineClient({
username: username,
password: password,
});

const session = await getSession();
await session.update((d) => {
d.userId = user.id;
});
} catch (err) {
return err as Error;
}
throw redirect("/");
}
export async function loginOrRegister(formData: FormData) {
const username = String(formData.get("username"));
const password = String(formData.get("password"));
const loginType = String(formData.get("loginType"));

let error = validateUsername(username) || validatePassword(password);
if (error) return new Error(error);

try {
const user = await (loginType !== "login" ? register(username, password) : login(username, password));

createRedmineClient({
username: username,
password: password,
});

const session = await getSession();
await session.update((d) => {
d.userId = user.id;
});
} catch (err) {
return err as Error;
}
throw redirect("/");
}
the problem I encouter now is, if the the function now returns the error, nodejs crashes with error message:
node:internal/process/promises:392
new UnhandledPromiseRejection(reason);
^

UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#<_Response>".
at throwUnhandledRejectionsMode (node:internal/process/promises:392:7)
at processPromiseRejections (node:internal/process/promises:475:17)
at process.processTicksAndRejections (node:internal/process/task_queues:106:32) {
code: 'ERR_UNHANDLED_REJECTION'
}
node:internal/process/promises:392
new UnhandledPromiseRejection(reason);
^

UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#<_Response>".
at throwUnhandledRejectionsMode (node:internal/process/promises:392:7)
at processPromiseRejections (node:internal/process/promises:475:17)
at process.processTicksAndRejections (node:internal/process/task_queues:106:32) {
code: 'ERR_UNHANDLED_REJECTION'
}
I just dont know where I can handle the rejections.. does anyone know, how to resolve that?
7 Replies
manubaun
manubaunOP4d ago
In my frontend I use it like this <form action={loginOrRegister} method="post"> and function is wrapped in an action export const loginOrRegister = action(lOR, "login");
Madaxen86
Madaxen864d ago
Maybe just return the redirect instead of throwing. At my phone, can verify atm
manubaun
manubaunOP4d ago
in the loginOrRegister function, if the validation fails it create a new error and returns it. then the same issue happends ERR_UNHANDLED_REJECTION so its not even reaching redirect
Madaxen86
Madaxen863d ago
Can't reproduce it. Maybe you can in a stackblitz? https://stackblitz.com/edit/github-52ahab?file=src%2Froutes%2Findex.tsx,src%2Fserver.ts What's your router version? Unrelated: You'll probaly need to "use server" to the function to be able to get and update the session.
manubaun
manubaunOP2d ago
used your code and yes, I cannot reproduce it either. Then tracked down the error. In my navbar I use a function called getUsers (all from the with-drizzle example) there it uses const user = createAsync(async () => getUser(), { deferStream: true }); this is somehow the function, where it all goes bad. I probably use createAsync wrong. https://stackblitz.com/edit/github-52ahab-xybqhj?file=src%2Froutes%2Findex.tsx,src%2Fserver.ts,src%2Fcomponents%2FNav.tsx note: In my Stackbliz example I get "Context is not available". On my Mac, I getht the ERR_UNHANDLED_REJECTION crash
StackBlitz
Solid-start With Tailwindcss Example (forked) - StackBlitz
Run official live example code for Solid-start With Tailwindcss, created by Solidjs on StackBlitz
manubaun
manubaunOP2d ago
the idea is, to show some extra infos and options when user is logged in. in the drizzle example it uses it on a page like here:
import { createAsync, type RouteDefinition } from "@solidjs/router";
import { getUser, logout } from "~/api";

export const route = {
preload() {
getUser();
}
} satisfies RouteDefinition;

export default function Home() {
const user = createAsync(async () => getUser(), { deferStream: true });
return (
<main class="w-full p-4 space-y-2">
<h2 class="font-bold text-3xl">Hello {user()?.username}</h2>
<h3 class="font-bold text-xl">Message board</h3>
<form action={logout} method="post">
<button name="logout" type="submit">
Logout
</button>
</form>
</main>
);
}
import { createAsync, type RouteDefinition } from "@solidjs/router";
import { getUser, logout } from "~/api";

export const route = {
preload() {
getUser();
}
} satisfies RouteDefinition;

export default function Home() {
const user = createAsync(async () => getUser(), { deferStream: true });
return (
<main class="w-full p-4 space-y-2">
<h2 class="font-bold text-3xl">Hello {user()?.username}</h2>
<h3 class="font-bold text-xl">Message board</h3>
<form action={logout} method="post">
<button name="logout" type="submit">
Logout
</button>
</form>
</main>
);
}
`
Madaxen86
Madaxen862d ago
Got it. You can not export actions and query from files with top level "use server", because they need to run on the client. Move them to another file or place the "use server" directive at the top of the async functions.
Want results from more Discord servers?
Add your server