Skip to content

Commit

Permalink
fix(app): properly log all errors
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiDood committed Dec 8, 2024
1 parent 037c00a commit b3eb737
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,23 @@ export async function handle({ event, resolve }) {
}),
};

// eslint-disable-next-line init-declarations
let response: Response;

const start = performance.now();
try {
response = await resolve(event);
} catch (err) {
if (isValiError(err)) {
const valibotErrorPaths = err.issues.map(issue => getDotPath(issue)).filter(path => path !== null);
event.locals.ctx.logger.fatal({ valibotError: err, valibotErrorPaths }, 'valibot validation failed');
} else if (err instanceof AssertionError) {
event.locals.ctx.logger.fatal({ nodeAssertionError: err }, 'assertion error encountered');
const response = await resolve(event);
const requestTimeMillis = performance.now() - start;
event.locals.ctx.logger.info({ requestTimeMillis });
return response;
}

export function handleError({ error, event }) {
if (typeof event.locals.ctx !== 'undefined') {
if (isValiError(error)) {
const valibotErrorPaths = error.issues.map(issue => getDotPath(issue)).filter(path => path !== null);
event.locals.ctx.logger.fatal({ valibotError: error, valibotErrorPaths }, 'valibot validation failed');
} else if (error instanceof AssertionError) {
event.locals.ctx.logger.fatal({ nodeAssertionError: error }, 'assertion error encountered');
} else {
event.locals.ctx.logger.fatal(err, 'unknown error encountered');
event.locals.ctx.logger.fatal(error, 'unknown error encountered');
}
throw err;
} finally {
const requestTimeMillis = performance.now() - start;
event.locals.ctx.logger.info({ requestTimeMillis });
}

return response;
throw error;
}

0 comments on commit b3eb737

Please sign in to comment.