Skip to content

Commit

Permalink
fix: error handling cors headers
Browse files Browse the repository at this point in the history
  • Loading branch information
stephancill committed Mar 20, 2024
1 parent 610ae30 commit 68b55ca
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function handlePost(req: Request) {
const body = await req.json();
console.log(`Processing POST request for ${url}`);
if (!url) {
return new Response('Missing url query param', { status: 400 });
return new Response('Missing url query param', { status: 400, headers: CORS_HEADERS });
}
const data = await postAndExtract(url, body);

Expand Down
14 changes: 7 additions & 7 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ export async function handleRequest(req: Request): Promise<Response> {
const path = getRequestPath(req);
if (req.method === 'GET') {
if (path === '/media') {
return handleMedia(req);
return await handleMedia(req);
}

if (path === '/') {
return handleGet(req);
return await handleGet(req);
}
}

if (req.method === 'POST') {
if (path === '/redirect') {
return handleRedirect(req);
return await handleRedirect(req);
}

return handlePost(req);
return await handlePost(req);
}
} catch (e) {
if (e instanceof ErrorResponse) {
return Response.json({ error: e.message }, { status: e.statusCode });
return Response.json({ error: e.message }, { status: e.statusCode, headers: CORS_HEADERS });
}
return Response.json({ error: e }, { status: 500 });
return Response.json({ error: e }, { status: 500, headers: CORS_HEADERS });
}

return new Response('Not found', { status: 404 });
return new Response('Not found', { status: 404, headers: CORS_HEADERS });
}

0 comments on commit 68b55ca

Please sign in to comment.