Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catch exceptions in handler function and wait until handler completes #251

Merged
merged 2 commits into from
Jul 19, 2024

Conversation

karthik2804
Copy link
Collaborator

This PR adds a try...catch to the template so that any unhandled exceptions are caught and returned as an error instead of the generic error along the lines of event loop error - both task and job queues are empty, but expected operations did not resolveInternal error.

This PR also modifies the template so that there is an event.waitUntill on the handler function so that applications can do some work even after the response is sent.

@karthik2804
Copy link
Collaborator Author

@tschneidereit I have made the change as discussed.

Comment on lines 22 to 25
// In case you want to do sonme work after the response is sent
// uncomment the line below and comment out the line with
// `await handler(event.request, res)
// event.waitUntil(handler(event.request, res))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy to replace this with a boolean and if...else statement if that would be cleaner.

@@ -18,7 +18,16 @@ async function handleEvent(event: FetchEvent) {

let res = new ResponseBuilder(resolve);

await handler(event.request, res)
try {
// In case you want to do sonme work after the response is sent
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

try {
// In case you want to do sonme work after the response is sent
// uncomment the line below and comment out the line with
// `await handler(event.request, res)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra '`' symbol here

} catch (e: any) {
res.status(500)
res.send(`error in handler: ${e}`)
}
}

// Keep wizer happy during pre-init. Should go away
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space on the line below

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue linked here is closed -- is this still needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will clean it up. Thoughts on the comment vs using a if...else if you want to continue processing after response has been sent back?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really have a preference.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving it as a comment, since it is a template, we can change it if we feel the need for it.

@karthik2804 karthik2804 merged commit e1a417a into fermyon:feat/sdk-v2 Jul 19, 2024
2 checks passed
@karthik2804 karthik2804 deleted the handle_errors branch July 19, 2024 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants