Skip to content

Commit

Permalink
feat: add req to onError handler for response validation (#564)
Browse files Browse the repository at this point in the history
* feat: add req to onError handler for response validation

* docs: add docs on onError response validation handler
  • Loading branch information
0xCAFEADD1C7 authored Mar 21, 2021
1 parent 69cdc9f commit 52d81a0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,15 @@ Determines whether the validator should validate responses. Also accepts respons

**onError:**

A function that will be invoked on response validation error, instead of the default handling. Useful if you want to log an error or emit a metric, but don't want to actually fail the request. Receives the validation error and offending response body.
A function that will be invoked on response validation error, instead of the default handling. Useful if you want to log an error or emit a metric, but don't want to actually fail the request. Receives the validation error, the offending response body, and the express request object.

For example:

```
validateResponses: {
onError: (error, body) => {
onError: (error, body, req) => {
console.log(`Response body fails validation: `, error);
console.log(`Emitted from:`, req.originalUrl);
console.debug(body);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type ValidateRequestOpts = {
export type ValidateResponseOpts = {
removeAdditional?: boolean | 'all' | 'failing';
coerceTypes?: boolean | 'array';
onError?: (err: InternalServerError, json: any) => void;
onError?: (err: InternalServerError, json: any, req: Request) => void;
};

export type ValidateSecurityOpts = {
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/openapi.response.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class ResponseValidator {
} catch (err) {
// If a custom error handler was provided, we call that
if (err instanceof InternalServerError && this.eovOptions.onError) {
this.eovOptions.onError(err, body)
this.eovOptions.onError(err, body, req)
} else {
// No custom error handler, or something unexpected happen.
throw err;
Expand Down
7 changes: 5 additions & 2 deletions test/response.validation.on.error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ describe(packageJson.name, () => {
.then((r: any) => {
const data = [{ id: 'bad_id', name: 'name', tag: 'tag' }];
expect(r.body).to.eql(data);
expect(onErrorArgs.length).to.equal(2);
expect(onErrorArgs.length).to.equal(3);
expect(onErrorArgs[0].message).to.equal('.response[0].id should be integer');
expect(onErrorArgs[1]).to.eql(data);
expect(onErrorArgs[2].query).to.eql({
mode: 'bad_type'
});
}));

it('custom error handler not invoked on valid response', async () =>
Expand All @@ -86,7 +89,7 @@ describe(packageJson.name, () => {
.then((r: any) => {
const data = [{ id: 'bad_id_throw', name: 'name', tag: 'tag' }];
expect(r.body.message).to.equal('error in onError handler');
expect(onErrorArgs.length).to.equal(2);
expect(onErrorArgs.length).to.equal(3);
expect(onErrorArgs[0].message).to.equal('.response[0].id should be integer');
expect(onErrorArgs[1]).to.eql(data);
}));
Expand Down

0 comments on commit 52d81a0

Please sign in to comment.