Contain a throw from the application's logger instead of letting it escape

This commit is contained in:
2026-09-04 11:33:20 +02:00
parent 80530c6265
commit 2e422c7584
5 changed files with 51 additions and 10 deletions
+23
View File
@@ -20,3 +20,26 @@ export const silentLog: SmppLog = {
verbose: noop,
warn: noop,
};
function contained(log: SmppLog, method: keyof SmppLog): LogMethod {
return (msg, metadata) => {
try {
log[method](msg, metadata);
} catch {
// Reporting a broken logger would need a logger, and hard rule 1 outranks the report.
}
};
}
/** The application's logger with a throw from it contained, or silence when it supplied none. */
export function guardedLog(log?: SmppLog): SmppLog {
if (log === undefined) return silentLog;
return {
debug: contained(log, 'debug'),
error: contained(log, 'error'),
info: contained(log, 'info'),
verbose: contained(log, 'verbose'),
warn: contained(log, 'warn'),
};
}