Custom error handler for request validation
See original GitHub issueHello. 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({
...createError({
type: err.name,
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:
- Created 3 years ago
- Reactions:4
- Comments:6 (4 by maintainers)
Top 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 >
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’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)
Reopening as this request. It is a related, but different from that referenced above