Skip to content

Commit

Permalink
fix: improved handling of errors in response validation (#201)
Browse files Browse the repository at this point in the history
* fix: async function broke delayed responses

* fix: improved handling of errors in response validation
  • Loading branch information
richardruiter committed Oct 26, 2022
1 parent da23c41 commit 23622c5
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/validation/OpenApiAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,41 @@ export default class OpenApiAdapter extends ValidationAdapter {
const currentRoute = this.findRoute(req);

if (currentRoute && currentRoute[method]) {
// check response
const responseValidator = new OpenAPIResponseValidator(
currentRoute[method]
);
try {
// check response
const responseValidator = new OpenAPIResponseValidator(
currentRoute[method]
);

const result = responseValidator.validateResponse(
response.status,
JSON.parse(response.body)
);
const result = responseValidator.validateResponse(
response.status,
JSON.parse(response.body)
);

if (result?.errors.length > 0) {
const error = createHttpError(
409,
new Error(JSON.stringify(result.errors, null, 2))
if (result?.errors.length > 0) {
const error = createHttpError(
409,
new Error(JSON.stringify(result.errors, null, 2))
);
return {
valid: false,
error,
};
}
} catch (err) {
const error = new createHttpError[500](
JSON.stringify(
[
{
path: "schema",
errorCode: "type.openapi.error",
message: err.message,
location: "schema",
},
],
null,
2
)
);
return {
valid: false,
Expand Down

0 comments on commit 23622c5

Please sign in to comment.