error handling robustness, logger corrections, better playlist-editable checking

This commit is contained in:
2025-03-14 20:44:37 -07:00
parent ef3a055c06
commit 14eeb57a0e
4 changed files with 76 additions and 95 deletions

View File

@@ -2,36 +2,22 @@ import path from "path";
import { createLogger, transports, config, format, type Logger } from "winston";
const { combine, timestamp, printf, errors } = format;
const { combine, timestamp, printf } = format;
const metaFormat = (meta: object) => {
const disallowedKeySets = [{ type: Error, keys: ["stack"] }];
if (Object.keys(meta).length > 0)
return (
"\n" +
JSON.stringify(
meta,
Object.getOwnPropertyNames(meta).filter((key) => {
for (const pair of disallowedKeySets) {
if (meta instanceof pair.type) {
return !pair.keys.includes(key);
}
}
return true;
}),
"\t"
)
);
return "\n" + JSON.stringify(meta, null, "\t");
return "";
};
const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
const logFormat = printf(({ level, message, timestamp, stack, ...meta }) => {
const errorObj: Error = meta["error"] as Error;
if (errorObj) {
const stackStr = errorObj["stack"];
return (
`${timestamp} [${level.toUpperCase()}]: ${message}` + // line 1
`${metaFormat(errorObj)}\n` + // metadata
`${errorObj["stack"] ?? ""}` // stack trace if any
`${stackStr}` // stack trace if any
);
}
return `${timestamp} [${level.toUpperCase()}]: ${message}${metaFormat(meta)}`;
@@ -39,11 +25,7 @@ const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
const winstonLogger: Logger = createLogger({
levels: config.npm.levels,
format: combine(
errors({ stack: true }),
timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
logFormat
),
format: combine(timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), logFormat),
transports: [
new transports.Console({ level: "info" }),
new transports.File({
@@ -58,7 +40,7 @@ const winstonLogger: Logger = createLogger({
],
});
winstonLogger.on("error", (error) =>
winstonLogger.error("Error inside logger", { error })
console.error("Error inside logger", error)
);
winstonLogger.exceptions.handle(
new transports.File({