forked from JavaScriptSolidServer/JavaScriptSolidServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-handler.js
More file actions
24 lines (24 loc) · 842 Bytes
/
error-handler.js
File metadata and controls
24 lines (24 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Top-level Fastify error handler that logs the full stack for any 5xx.
*
* Without this, 500 responses carry only Fastify's default 91-byte body and
* leave no trace in logs — production debugging has to infer the exception
* from the response body alone (see #309 investigation for why that's bad).
*
* 4xx errors carry their own statusCode and don't need stack logging — they
* are expected client errors with self-explanatory messages.
*/
export function registerErrorHandler(fastify) {
fastify.setErrorHandler(function (err, request, reply) {
const statusCode = err.statusCode ?? 500;
if (statusCode >= 500) {
request.log.error({
err,
method: request.method,
url: request.url,
hostname: request.hostname
}, 'Unhandled 5xx error');
}
reply.send(err);
});
}