diff --git a/package.json b/package.json index 993722e..1b26e1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wms-core", - "version": "0.7.6", + "version": "0.7.12", "description": "Node web framework", "repository": "git@gitlab.com:ArisuOngaku/wms-core.git", "author": "Alice Gaudon ", diff --git a/src/Application.ts b/src/Application.ts index 03697e4..43640f8 100644 --- a/src/Application.ts +++ b/src/Application.ts @@ -1,4 +1,4 @@ -import express, {NextFunction, Request, Response, Router} from 'express'; +import express, {NextFunction, Request, RequestHandler, Response, Router} from 'express'; import {BadRequestError, HttpError, NotFoundHttpError, ServerError, ServiceUnavailableHttpError} from "./HttpError"; import {lib} from "nunjucks"; import Logger from "./Logger"; @@ -170,6 +170,12 @@ export default abstract class Application { }); } + public setupRequestParsingMiddlewares(router: Router) { + for (const controller of this.controllers) { + controller.setupRequestParsingMiddlewares(router); + } + } + public getWebSocketListeners(): { [p: string]: WebSocketListener } { return this.webSocketListeners; } diff --git a/src/Controller.ts b/src/Controller.ts index bed86e3..a47c2a8 100644 --- a/src/Controller.ts +++ b/src/Controller.ts @@ -39,6 +39,9 @@ export default abstract class Controller { return []; } + public setupRequestParsingMiddlewares(router: Router): void { + } + public hasGlobalHandlers(): boolean { return this.getGlobalHandlers().length > 0; } diff --git a/src/components/ExpressAppComponent.ts b/src/components/ExpressAppComponent.ts index 71bba51..3c1b495 100644 --- a/src/components/ExpressAppComponent.ts +++ b/src/components/ExpressAppComponent.ts @@ -1,7 +1,9 @@ import ApplicationComponent from "../ApplicationComponent"; -import express, {Express, Router} from "express"; +import express, {Express, NextFunction, Request, Response, Router} from "express"; import Logger from "../Logger"; import {Server} from "http"; +import {MulterError} from "multer"; +import {FileError, ValidationBag} from "../db/Validator"; export default class ExpressAppComponent extends ApplicationComponent { private readonly port: number; @@ -17,6 +19,22 @@ export default class ExpressAppComponent extends ApplicationComponent { Logger.info(`Web server running on localhost:${this.port}.`); }); + this.app?.setupRequestParsingMiddlewares(router); + + // Multer error handler + router.use((err: any, req: Request, res: Response, next: NextFunction) => { + if (err instanceof MulterError) { + const bag = new ValidationBag(); + const validationError = new FileError(err.message); + validationError.thingName = err.field; + bag.addMessage(validationError); + req.flash('validation', bag.getMessages()); + res.redirectBack(); + } else { + next(err); + } + }); + router.use(express.json()); router.use(express.urlencoded({ extended: true, diff --git a/src/components/LogRequestsComponent.ts b/src/components/LogRequestsComponent.ts index 7ea9ea9..e4285db 100644 --- a/src/components/LogRequestsComponent.ts +++ b/src/components/LogRequestsComponent.ts @@ -34,6 +34,7 @@ export default class LogRequestsComponent extends ApplicationComponent { query: req.query, params: req.params, body: req.body, + file: req.file, files: req.files, cookies: req.cookies, sessionId: req.sessionID, diff --git a/src/db/Validator.ts b/src/db/Validator.ts index f4809d3..9f0706e 100644 --- a/src/db/Validator.ts +++ b/src/db/Validator.ts @@ -409,4 +409,17 @@ export class UnknownRelationValidationError extends ValidationError { public get message(): string { return `${this.thingName}=${this.value} relation was not found in ${this.table}${this.foreignKey !== undefined ? `.${this.foreignKey}` : ''}.`; } -} \ No newline at end of file +} + +export class FileError extends ValidationError { + private readonly m: string; + + constructor(message: string) { + super(); + this.m = message; + } + + public get message(): string { + return `${this.m}`; + } +}