Middleware for catching validation errors
See original GitHub issueWe define a lot of different rules across multiple files and wanted to have a single middleware to handle all express-validator errors. So we came up with putting this in the express app like:
this.app.use((req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
next();
});
As the validations are provided on the routes they seem to be undefined at hat point in the chain. Is there a clean way to have the response handling for errors in a centralized way?
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
What are error handling and validation architecture in .NET ...
NET core Middleware to catch all exceptions and create HTTP error status according to Exception Type (in the ConfigurationExceptionType method) and build error...
Read more >Express Validation and Error Handling
Learn how to validate requests and handle errors using Node ... Validate API requests and display HTML form errors using custom middleware.
Read more >custom error middleware in net core not catching model state ...
I have some custom middleware set up to handle errors in my api ... the model state validation error is not getting caught...
Read more >Guzzle Middleware to Handle External Validation Failures
In this post I want to show how we use Guzzle middleware with Guzzle and Laravel Http clients to catch 422 validation failures...
Read more >Error handling and validation architecture in .NET Core - ITNEXT
In many projects error handling and validation is distributed across business logic, API controllers, data access layer in the form of conditions (“if-else” ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I also tried to have a single middleware, but not work
And then I create something like:
Thank you. We implemented this as suggested. Works stable for quite some time.