1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-02 09:12:07 +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
};
type logFN = (...args: (string | number)[]) => {};
export type logFN = (...args: (string | number)[]) => {};
export const Logger: {
error: logFN,

View File

@ -7,6 +7,7 @@ import {NotificationManager} from '../model/NotifocationManager';
import {Logger} from '../Logger';
import {SharingDTO} from '../../common/entities/SharingDTO';
import {Utils} from '../../common/Utils';
import {LoggerRouter} from '../routes/LoggerRouter';
export class RenderingMWs {
@ -92,6 +93,7 @@ export class RenderingMWs {
if (err instanceof ErrorDTO) {
if (err.details) {
Logger.warn('Handled error:');
LoggerRouter.log(Logger.warn, req, res);
console.log(err);
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);
return res.json(message);
}
NotificationManager.error('unknown server error', err);
NotificationManager.error('Unknown server error', err);
return next(err);
}

View File

@ -1,5 +1,5 @@
import {Express, NextFunction, Request, Response} from 'express';
import {Logger} from '../Logger';
import {logFN, Logger} from '../Logger';
declare global {
namespace Express {
@ -14,43 +14,34 @@ declare global {
* Adds logging to express
*/
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) {
app.get('/api*', (req: Request, res: Response, next: NextFunction) => {
req._startTime = Date.now();
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');
};
LoggerRouter.log(Logger.verbose, req, res);
return next();
});
app.get('/node_modules*', (req: Request, res: Response, next: NextFunction) => {
req._startTime = Date.now();
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');
};
LoggerRouter.log(Logger.silly, req, res);
return next();
});
app.use((req: Request, res: Response, next: NextFunction) => {
if (req.logged === true) {
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');
};
LoggerRouter.log(Logger.debug, req, res);
return next();
});

View File

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