Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add req to onError handler for response validation #564

Merged
merged 2 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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