SolidJSS
SolidJS13mo ago
32 replies
danchez

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
.

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'
}

Here is the function:

export async function loginOrRegister(formData: FormData) {
  const username = String(formData.get("username"));
  const password = String(formData.get("password"));

  let error = validateUsername(username) || validatePassword(password);

  if (error) {
    // Promise.reject(new Error(error)). // <--- does not cause rejection, but UI does not show error message
    return new Error(error); // <--- UI shows error message but dev server stops due to UnhandledPromiseRejection
  }

  try {
    const user = await login(username, password);
    const session = await getSession();
    await session.update((d) => {
      d.userId = user.id;
    });
  } catch (err) {
    return err as Error;
  }

  throw redirect("/");
}


Not sure exactly what's going on and hoping someone can perhaps provide insight.
Was this page helpful?