Skip to content
25 changes: 23 additions & 2 deletions src/backend/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Config} from '../common/config/private/Config';
import {LogLevel} from '../common/config/private/PrivateConfig';
import {ErrorCodes} from '../common/entities/Error';

export type logFN = (...args: (string | number | (() => string))[]) => void;

const forcedDebug = process.env['NODE_ENV'] === 'debug';

Expand All @@ -11,7 +11,7 @@ if (forcedDebug === true) {
);
}

export type LoggerArgs = (string | number | (() => string))
export type LoggerArgs = (string | number | (() => string) | Record<any, unknown> | Error);
export type LoggerFunction = (...args: LoggerArgs[]) => void;

export interface ILogger {
Expand Down Expand Up @@ -103,4 +103,25 @@ export class Logger {
});
console.log(date + tag + LOG_TAG, ...args);
}

public static logLevelForError(e: ErrorCodes): LoggerFunction {
switch (e) {
case ErrorCodes.INPUT_ERROR:
case ErrorCodes.NOT_AUTHENTICATED:
case ErrorCodes.ALREADY_AUTHENTICATED:
case ErrorCodes.NOT_AUTHORISED:
case ErrorCodes.PERMISSION_DENIED:
case ErrorCodes.CREDENTIAL_NOT_FOUND:
return Logger.debug
case ErrorCodes.SETTINGS_ERROR:
case ErrorCodes.TASK_ERROR:
case ErrorCodes.JOB_ERROR:
case ErrorCodes.THUMBNAIL_GENERATION_ERROR:
case ErrorCodes.PHOTO_GENERATION_ERROR:
case ErrorCodes.SERVER_ERROR:
return Logger.error
default:
return Logger.warn
}
}
}
13 changes: 3 additions & 10 deletions src/backend/middlewares/RenderingMWs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,12 @@ export class RenderingMWs {
): void {
if (err instanceof ErrorDTO) {
if (err.details) {
Logger.warn('Handled error:');
LoggerRouter.log(Logger.warn, req, res);
const logFn = Logger.logLevelForError(err.code)
LoggerRouter.log(logFn, req, res);
// use separate rendering for detailsStr
const d = err.detailsStr;
delete err.detailsStr;
console.log(err);
if (err.detailsStr) {
try {
console.log('details:', JSON.stringify(err.detailsStr));
} catch (_) {
console.log(err.detailsStr);
}
}
logFn(err);
err.detailsStr = d;
delete err.details; // do not send back error object to the client side

Expand Down
4 changes: 2 additions & 2 deletions src/backend/routes/LoggerRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Express, NextFunction, Request, Response} from 'express';
import {logFN, Logger} from '../Logger';
import {LoggerFunction, Logger} from '../Logger';
import {Config} from '../../common/config/private/Config';

declare global {
Expand All @@ -16,7 +16,7 @@ declare global {
* Adds logging to express
*/
export class LoggerRouter {
public static log(loggerFn: logFN, req: Request, res: Response): void {
public static log(loggerFn: LoggerFunction, req: Request, res: Response): void {
if (req.logged === true) {
return;
}
Expand Down