Skip to content

Commit d49d814

Browse files
Merge pull request #2161 from balena-io/avoid-headers-already-sent
Device logs: avoid errors related to headers already sent
2 parents 367bca5 + 7fdde53 commit d49d814

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/features/device-logs/lib/store.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,19 @@ export const storeStream =
121121
};
122122

123123
function handleStoreErrors(req: Request, res: Response, err: Error) {
124-
if (handleHttpErrors(req, res, err)) {
125-
return;
124+
if (!(err instanceof errors.HttpError)) {
125+
// Log non `HttpError`s regardless of headers state
126+
captureException(err, 'Failed to store device logs');
127+
}
128+
if (res.headersSent) {
129+
// If headers are already sent, we can't change the response code so we just end the response
130+
res.end();
131+
} else {
132+
if (handleHttpErrors(req, res, err)) {
133+
return;
134+
}
135+
res.status(500).end();
126136
}
127-
captureException(err, 'Failed to store device logs');
128-
res.status(500).end();
129137
}
130138

131139
const primaryBackend = await getPrimaryBackend();

0 commit comments

Comments
 (0)