1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-10 11:10:35 +02:00

Improving error logging for handled server side errors.

Looking for: "ForbiddenError: invalid csrf token"
This commit is contained in:
Patrik J. Braun 2020-12-25 10:36:49 +01:00
parent ee355dd012
commit 9df2b8fbaa
4 changed files with 25 additions and 29 deletions

View File

@ -30,7 +30,7 @@ export const winstonSettings = {
exitOnError: false exitOnError: false
}; };
type logFN = (...args: (string | number)[]) => {}; export type logFN = (...args: (string | number)[]) => {};
export const Logger: { export const Logger: {
error: logFN, error: logFN,

View File

@ -7,6 +7,7 @@ import {NotificationManager} from '../model/NotifocationManager';
import {Logger} from '../Logger'; import {Logger} from '../Logger';
import {SharingDTO} from '../../common/entities/SharingDTO'; import {SharingDTO} from '../../common/entities/SharingDTO';
import {Utils} from '../../common/Utils'; import {Utils} from '../../common/Utils';
import {LoggerRouter} from '../routes/LoggerRouter';
export class RenderingMWs { export class RenderingMWs {
@ -92,6 +93,7 @@ export class RenderingMWs {
if (err instanceof ErrorDTO) { if (err instanceof ErrorDTO) {
if (err.details) { if (err.details) {
Logger.warn('Handled error:'); Logger.warn('Handled error:');
LoggerRouter.log(Logger.warn, req, res);
console.log(err); console.log(err);
delete (err.details); // do not send back error object to the client side delete (err.details); // do not send back error object to the client side
@ -103,7 +105,7 @@ export class RenderingMWs {
const message = new Message<any>(err, null); const message = new Message<any>(err, null);
return res.json(message); return res.json(message);
} }
NotificationManager.error('unknown server error', err); NotificationManager.error('Unknown server error', err);
return next(err); return next(err);
} }

View File

@ -1,5 +1,5 @@
import {Express, NextFunction, Request, Response} from 'express'; import {Express, NextFunction, Request, Response} from 'express';
import {Logger} from '../Logger'; import {logFN, Logger} from '../Logger';
declare global { declare global {
namespace Express { namespace Express {
@ -14,43 +14,34 @@ declare global {
* Adds logging to express * Adds logging to express
*/ */
export class LoggerRouter { export class LoggerRouter {
public static log(loggerFn: logFN, req: Request, res: Response) {
if (req.logged === true) {
return;
}
req.logged = true;
req._startTime = Date.now();
const end = res.end;
res.end = (a?: any, b?: any, c?: any) => {
res.end = end;
res.end(a, b, c);
loggerFn(req.method, req.url, res.statusCode, (Date.now() - req._startTime) + 'ms');
};
}
public static route(app: Express) { public static route(app: Express) {
app.get('/api*', (req: Request, res: Response, next: NextFunction) => { app.get('/api*', (req: Request, res: Response, next: NextFunction) => {
req._startTime = Date.now(); LoggerRouter.log(Logger.verbose, req, res);
req.logged = true;
const end = res.end;
res.end = (a?: any, b?: any, c?: any) => {
res.end = end;
res.end(a, b, c);
Logger.verbose(req.method, req.url, res.statusCode, (Date.now() - req._startTime) + 'ms');
};
return next(); return next();
}); });
app.get('/node_modules*', (req: Request, res: Response, next: NextFunction) => { app.get('/node_modules*', (req: Request, res: Response, next: NextFunction) => {
req._startTime = Date.now(); LoggerRouter.log(Logger.silly, req, res);
req.logged = true;
const end = res.end;
res.end = (a?: any, b?: any, c?: any) => {
res.end = end;
res.end(a, b, c);
Logger.silly(req.method, req.url, res.statusCode, (Date.now() - req._startTime) + 'ms');
};
return next(); return next();
}); });
app.use((req: Request, res: Response, next: NextFunction) => { app.use((req: Request, res: Response, next: NextFunction) => {
if (req.logged === true) { LoggerRouter.log(Logger.debug, req, res);
return next();
}
req._startTime = Date.now();
const end = res.end;
res.end = (a?: any, b?: any, c?: any) => {
res.end = end;
res.end(a, b, c);
Logger.debug(req.method, req.url, res.statusCode, (Date.now() - req._startTime) + 'ms');
};
return next(); return next();
}); });

View File

@ -26,6 +26,9 @@ export class ShareSettingsService extends AbstractSettingsService<ClientConfig.S
public getSharingList(): Promise<SharingDTO[]> { public getSharingList(): Promise<SharingDTO[]> {
if (!this._settingsService.settings.value.Client.Sharing.enabled) {
return Promise.resolve([]);
}
return this._networkService.getJson('/share/list'); return this._networkService.getJson('/share/list');
} }