Globally catch exceptions


Is there a way to globally catch exceptions? I’m seeing a couple exceptions here and there in our charts on the CF dashboard, but don’t see an easy way to actually see what those exceptions were. And I’m not sure if wrapping all of our code in a try/catch is the best way to go about it

There’s event.passThroughOnException() though this does not catch the exception per se but just continues without stopping the request, so I guess thats not necessarly what you were looking after.

As far as I can tell there wouldnt be a global exception handler, so a global try would be the way forward, however keep in mind this wont catch errors of asynchronous calls. Even though archived at this point, addresses your question.

1 Like

Just like @sandro pointed out, it’s easy to get lost in async calls, you have to catch them properly.

Link to the new debug docs:

Thanks for the info!
I’ll start catching my async calls properly.
As for the global try, I’m currently using webpack so do you know of any way to wrap the compiled code easily? Currently if I trigger the wrangler deploy, it’ll automatically trigger webpack and immediately upload the output, so I can’t manually add a try to it afterwards

Shouldnt it be sufficient to simply place the try in the initial file where you set up the worker code?

Sorry for the late response. But no, I’ve yesterday added a try/catch around my whole webpack bundle, and unfortunately I’m still unable to catch the edge cases that users are running into.

It would be super awesome if Cloudflare even showed a small sample of the errors a worker encounters, as it’s not always easy to just catch edge cases with a try/catch, especially when you’re working with webpack and third-party libraries

If these are exceptions which happen in asynchronous code, which was not called via await, you cant use try but - as I mentioned - will have to use the appropriate error handlers.