Skip to content

Commit

Permalink
src: return message and stack trace on unhandled error in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Sep 27, 2023
1 parent 808c8d2 commit 636353b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface Env {
/**
* Environment the worker is running in
*/
ENVIRONMENT: 'dev' | 'staging' | 'prod';
/**
* R2 bucket we read from
*/
Expand Down
33 changes: 23 additions & 10 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,29 @@ interface Worker {
const cloudflareWorker: Worker = {
fetch: async (request, env, ctx) => {
const cache = caches.default;
switch (request.method) {
case 'HEAD':
case 'GET':
return handlers.get(request, env, ctx, cache);
case 'POST':
return handlers.post(request, env, ctx, cache);
case 'OPTIONS':
return handlers.options(request, env, ctx, cache);
default:
return responses.METHOD_NOT_ALLOWED;
try {
switch (request.method) {
case 'HEAD':
case 'GET':
return handlers.get(request, env, ctx, cache);
case 'POST':
return handlers.post(request, env, ctx, cache);
case 'OPTIONS':
return handlers.options(request, env, ctx, cache);
default:
return responses.METHOD_NOT_ALLOWED;
}
} catch (e) {
let responseBody = 'Internal Server Error';

if (env.ENVIRONMENT === 'dev' && e instanceof Error) {
responseBody += `\nMessage: ${e.message}\nStack trace: ${e.stack}`;
}

return new Response(responseBody, {
status: 500,
headers: { 'cache-control': 'no-store' },
});
}
},
};
Expand Down

0 comments on commit 636353b

Please sign in to comment.