diff --git a/src/Application.ts b/src/Application.ts index 77669f3..d1a6d5f 100644 --- a/src/Application.ts +++ b/src/Application.ts @@ -8,7 +8,7 @@ import MysqlConnectionManager from "./db/MysqlConnectionManager"; import Migration, {MigrationType} from "./db/Migration"; import {Type} from "./Utils"; import LogRequestsComponent from "./components/LogRequestsComponent"; -import {ValidationBag} from "./db/Validator"; +import {ValidationBag, ValidationError} from "./db/Validator"; import config from "config"; import * as fs from "fs"; import SecurityError from "./SecurityError"; @@ -91,7 +91,15 @@ export default abstract class Application implements Extendable { if (res.headersSent) return next(err); + // Transform single validation errors into a validation bag for convenience + if (err instanceof ValidationError) { + const bag = new ValidationBag(); + bag.addMessage(err); + err = bag; + } + if (err instanceof ValidationBag) { + const bag = err; res.format({ json: () => { res.status(401); @@ -99,15 +107,15 @@ export default abstract class Application implements Extendable { res.status(401); - res.send('Error: ' + err.getMessages()); + res.send('Error: ' + bag.getMessages()); }, html: () => { - req.flash('validation', err.getMessages()); + req.flash('validation', bag.getMessages()); res.redirectBack(); }, }); diff --git a/src/db/Validator.ts b/src/db/Validator.ts index 88fe9d9..9440456 100644 --- a/src/db/Validator.ts +++ b/src/db/Validator.ts @@ -313,7 +313,7 @@ export class ValidationBag extends Error { } } -export abstract class ValidationError extends Error { +export class ValidationError extends Error { public rawValueToHuman?: (val: V) => string; public thingName?: string; public value?: V;