Skip to content
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
4 changes: 3 additions & 1 deletion src/handlers/not-found.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const notFound = (_context, request, response) => {
response.status(404)
if (!response.headersSent) {
response.status(404)
}
return {
status: 404,
timestamp: new Date(),
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/request-validation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const requestValidation = (context, request, response) => {
response.status(400)
if (!response.headersSent) {
response.status(400)
}
return {
errors: context.validation.errors,
status: 400,
Expand Down
20 changes: 14 additions & 6 deletions src/handlers/response-validation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export default (logger, validateResponse) => (context, request, response) => {
// Prevent sending response if headers are already sent (e.g., after redirect)
if (response.headersSent) {
return undefined
}
Comment on lines +3 to +5
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This early return when headers are already sent is inconsistent with the pattern used in other handlers (unauthorized.js, request-validation.js, not-found.js), where the handler still returns a response object even if headers were already sent - they just skip setting the status code. Returning undefined here might prevent downstream code from understanding what should have been returned. Consider removing this early return and instead checking headersSent before each response.json/response.send/response.end call, which would allow the handler to still process and validate the response while avoiding sending headers twice.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback


const responseDoesntNeedValidation = response.statusCode >= 400
if (responseDoesntNeedValidation) {
return response.json(context.response)
Expand All @@ -22,12 +27,15 @@ export default (logger, validateResponse) => (context, request, response) => {
response: context.response
})
}
return response.status(502).json({
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 undefined
}
Comment on lines 30 to 39
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the validation-error path, falling back to response.end() when headersSent is true has the same risk of terminating an in-progress streamed/manual response. Prefer returning without writing, or only ending when the response hasn't already been ended (writableEnded/finished).

Copilot uses AI. Check for mistakes.

if (!context.response) {
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/unauthorized.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const unauthorized = async (context, request, response) => {
response.status(401)
if (!response.headersSent) {
response.status(401)
}
return {
status: 401,
timestamp: new Date(),
Expand Down
Loading