mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-09-24 00:37:20 -05:00
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import type { H3Error } from 'h3';
|
|
|
|
/**
|
|
* Logs server-side errors in a compact, readable format.
|
|
*
|
|
* Nitro fires the `error` hook for every request error, whatever the source
|
|
* (API routes, server routes/middleware, SSR rendering). It also fires for
|
|
* expected HTTP errors like 404s, so anything below a 500 is ignored here.
|
|
*/
|
|
export default defineNitroPlugin((nitroApp) => {
|
|
nitroApp.hooks.hook('error', (err, { event }) => {
|
|
const error = err as Partial<H3Error>;
|
|
const statusCode = error.statusCode ?? 500;
|
|
|
|
// Skip expected client errors (404, 401, 422, redirects, ...).
|
|
if (statusCode < 500) {
|
|
return;
|
|
}
|
|
|
|
const where = event ? `${event.method} ${event.path}` : 'non-request';
|
|
const tag = error.unhandled ? '[unhandled]' : '';
|
|
|
|
console.error(`[server error] ${tag} ${where} -> ${statusCode} ${err.message}`);
|
|
|
|
if (error.data !== undefined) {
|
|
console.error(' data:', error.data);
|
|
}
|
|
|
|
if (err.cause) {
|
|
console.error(' cause:', err.cause);
|
|
}
|
|
|
|
if (err.stack) {
|
|
// Drop the first line (already printed above as the message).
|
|
console.error(err.stack.split('\n').slice(1).join('\n'));
|
|
}
|
|
});
|
|
});
|