Open brettski opened 10 months ago
Update: file, https://github.com/ThatConference/thatconference.com/blob/next/feature/src/routes/api/auth/proxy/%2Bserver.js, updated so it properly returns 401 when not logged in.
export async function POST({ request, locals }) {
const body = await request.json();
const session = await locals.getSession();
const accessToken = session?.accessToken;
if (!accessToken) {
throw error(401, 'Unauthorized Access');
}
try {
...
Describe the bug when a call is made to
/api/auth/proxy
which is in error or if the user is not logged in (accessToken === undefined) the error is never making back to the user and the website hangs with the request. A true error does successfully get recorded in Sentry, makes it to the user's console, but not back to the web page.The file in next/feature: https://github.com/ThatConference/thatconference.com/blob/next/feature/src/routes/api/auth/proxy/%2Bserver.js
First issue is here (lines 9 - 15)
Having
throw error()
within a try block immediately goes to thecatch
block. The case above the message isundefined
and nothing makes back to the client, in console, Sentry or otherwise.A correction for this is as follows (new lines 9-16):
Now, when the user isn't logged in, Svelte does properly throw a 401, Unauthorized Access. A problem still is that the client doesn't get this error to the page. It does shows in in the console.
I am not sure how to restructure this to get the errors back to the user so the page isn't just hanging there.
To Reproduce Steps to reproduce the behavior:
Expected behavior In this repro the user should be alerted not authorized, or prompted to login (a little more difficult). For 500 errors, they should be relayed back to the user over the page just hanging there.
With the code as it is now,
/api/auth/proxy
is sending back a 500 error. This is because thethrow error(401...) is in the
tryblock, being caught by the catch block and trying to capture message
undefined`.The catch block (lines 42–49)
Catching
throw error(401, 'Unauthorized Access')
has no message, hence message's value isundefined
.