How can I catch client error event? #5894
Answered
by
AtilMohAmine
GwangIl-Park
asked this question in
Q&A
-
Can I catch client error event like POST, PUT, etc? I should catch client error like timeout, disconnect. So I tried like below in log middleware.
With GET method, it worked
But with other method, it did not work.. This is my testcode in server. For test with timeout, I use sleep.
I'm using express 4.18 and node version 18 |
Beta Was this translation helpful? Give feedback.
Answered by
AtilMohAmine
Nov 24, 2023
Replies: 1 comment
-
Here's an example of how you can configure timeout handling for your routes in Express: // Middleware to handle timeouts
app.use((req, res, next) => {
req.setTimeout(5000, () => {
const error = new Error('Request Timeout');
error.status = 408; // Request Timeout
next(error);
});
next();
});
// Your test route
app.post('/test', async (req: Request, res: Response) => {
// Simulating delay
await sleep(6000);
res.status(200).send();
});
// Error handling middleware
app.use((err, req, res, next) => {
if (res.headersSent) {
return next(err);
}
logger.error('Request Error:', err);
res.status(err.status || 500).json({ error: err.message });
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
IamLizu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an example of how you can configure timeout handling for your routes in Express: