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.

Custom error handler for request validation

See original GitHub issue

Hello. For our APIs we have error messages with certain structure. Therefore it would be nice to be able to define own error handler for request validation, similar to the onError prop for validateResponses you already have.

Proposed solution

We should have the possibility to define the handler as a property for the validateRequests. For example:

validateRequests: {
  onError: (error) => throw customError(error)

My current ugly workaround

Currently a catch the validation error in the error handling middleware, try to find out that this is really the validation error, format it and send to the client:

const errorHandler = (err, req, res, next) => {
  // do not want this checks
    if (err.errors && err.status) {
      return res.status(err.status).send({
          message: "Request violates API Specification",
          code: err.status,
        errors: err.errors,

Let me know what you think about it and if there are any better alternatives for my workaround!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

jjdonovcommented, Apr 20, 2021

I’d also be interested in an enhancement like this. The response validator middleware has an onError handler that lets you decide what to do with the error, and I think something similar could be useful for requests as well.

For example, simply logging some type of event and still allowing the request to continue. (In this use case, we are updating an old api that previously did not have an openapi spec and think some older clients in the wild might not conform to the spec exactly)

cdimasciocommented, Mar 17, 2021

Reopening as this request. It is a related, but different from that referenced above

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Error Message Handling for REST API - Baeldung
Implement a Global Exception Handler for a REST API with Spring. ... is thrown when an argument annotated with @Valid failed validation:.
Read more >
Request Validation and Custom Exception Handling in ...
Step 1: Create the Custom Exceptions · Step 2: Create the Exception Handler · Step 3: Specify Your Exception Handler in Settings ·...
Read more >
Handling Errors - FastAPI
When a request contains invalid data, FastAPI internally raises a RequestValidationError . And it also includes a default exception handler for it. To...
Read more >
Handling Request Validation Exceptions - Developer Notes
ASP.Net has a feature called “Custom Errors” which is the final catch in the framework for exceptions within an ASP.Net application. If the ......
Read more >
REST API Request Validation with Spring Boot
In this spring boot exception handler tutorial, we will learn to validate request body sent to PUT/POST REST APIs.
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 Post

No results found

github_iconTop Related Hashnode Post

No results found