Skip to content

Commit

Permalink
fix: error handling cors (#17)
Browse files Browse the repository at this point in the history
* fix: error handling cors headers

* docs(changeset): fix: handle errors and return correct CORS headers on error responses
  • Loading branch information
stephancill authored Mar 20, 2024
1 parent 610ae30 commit f3e8b46
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-oranges-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@open-frames/proxy": patch
---

fix: handle errors and return correct CORS headers on error responses
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 f3e8b46

Please sign in to comment.