question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware for catching validation errors

See original GitHub issue

We 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:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
edwinnnsscommented, Jul 27, 2018

I also tried to have a single middleware, but not work
And then I create something like:

const MiddlewareValidation = require('../middlewares/Validation')

const combineValidationMiddleware = validation => [validation, MiddlewareValidation]

app.post('/users', combineValidationMiddleware(UserController.validateStore), UserController.store)
1reaction
spech66commented, Oct 29, 2018

Thank you. We implemented this as suggested. Works stable for quite some time.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found