-
Hello, I would like to ask about the implementation of Sentry and error handling. As you might already know, in Express.js, Sentry can be integrated as follows: I have configured Sentry in the file where createConfig() is called, as shown below.
Additionally, even when I set up an error handler as follows, the errors are not being handled properly:
What is the correct way to set up error handling in Express.js in this context? Thank you for your assistance. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
Hello @yuuri111 , Are you using If so, the Also, the You need to make a custom result handler, similar to what you already made using
Assuming that you're using v19 or earlier, consider making a copy of the express-zod-api/src/result-handler.ts Line 69 in 7d865da But add a express-zod-api/src/result-handler.ts Line 109 in 7d865da Then do the following: const factory = new EndpointsFactory(customResultHandler); and I hope it helps. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your confirmation and response.
Yes, I'm using createServer().
Ideally, I would like to manage errors in a centralized manner, catching them all in one place, rather than setting Sentry.captureException in each handler. Therefore, I would like to try using attachRouting(). Currently, I am using ServerConfig instead of AppConfig as shown below (because I need to use beforeRouting, etc.).
In this case (using ServerConfig), how can I use attachRouting? Thank you for your assistance. |
Beta Was this translation helpful? Give feedback.
-
The original idea implies having one ResultHandler for consistent responses, but if they are not supposed to be consistent and you're having multiple ResultHandlers then yes, it's probably better to go with
Here is an article about that, @yuuri111 : https://github.com/RobinTail/express-zod-api/blob/master/README.md#connect-to-your-own-express-app |
Beta Was this translation helpful? Give feedback.
-
Related discussion #1907 |
Beta Was this translation helpful? Give feedback.
Hello @yuuri111 ,
Are you using
createServer()
method?If so, the
errorHandler
you put intocreateConfig()
is only used for the routing errors (when a route was not found). But the errors youthrow
from within your endpoints are handled by the corresponding ResultHandler of the EndpointsFactory that those endpoints were made on.Also, the
app.use
you call, is called after the proprietary error handler is installed, so it does not make any effect.You need to make a custom result handler, similar to what you already made using
createResultHandler
, but more comprehensive:Sentry.captureException
;Assuming…