Skip to content
Open
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
20 changes: 8 additions & 12 deletions src/handlers/response-validation.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export default (logger, validateResponse) => (context, request, response) => {
// Prevent sending headers if they're already sent
if (response.headersSent) {
return undefined
}

const responseDoesntNeedValidation = response.statusCode >= 400
if (responseDoesntNeedValidation) {
return response.json(context.response)
Expand All @@ -27,15 +22,16 @@ export default (logger, validateResponse) => (context, request, response) => {
response: context.response
})
}
const errorResponse = {
errors: valid.errors,
status: 502,
timestamp: new Date(),
message: 'Bad response'
}
if (!response.headersSent) {
return response.status(502).json({
errors: valid.errors,
status: 502,
timestamp: new Date(),
message: 'Bad response'
})
return response.status(502).json(errorResponse)
}
return undefined
return errorResponse
}

if (!context.response) {
Expand Down
Loading