Validator: add more information when exception is thrown in verifyStep

This commit is contained in:
Alice Gaudon 2020-08-26 14:56:34 +02:00
parent 9709aa1d46
commit 66ec3c0b47
2 changed files with 13 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{
"name": "wms-core",
"version": "0.20.5-rc.2",
"version": "0.20.5-rc.3",
"description": "Node web application framework and toolbelt.",
"repository": "https://gitlab.com/ArisuOngaku/wms-core",
"author": "Alice Gaudon <alice@gaudon.pro>",

View File

@ -2,6 +2,7 @@ import Model from "./Model";
import ModelQuery, {WhereTest} from "./ModelQuery";
import {Connection} from "mysql";
import {Type} from "../Utils";
import {ServerError} from "../HttpError";
export default class Validator<T> {
private readonly steps: ValidationStep<T>[] = [];
@ -27,8 +28,17 @@ export default class Validator<T> {
for (const step of this.steps) {
if (onlyFormat && !step.isFormat) continue;
const result = step.verifyStep(value, thingName, connection);
if ((result === false || result instanceof Promise && (await result) === false) && step.throw) {
let result;
try {
result = step.verifyStep(value, thingName, connection);
if (result instanceof Promise) {
result = await result;
}
} catch (e) {
throw new ServerError(`An error occurred while validating ${thingName} with value "${value}".`, e);
}
if (result === false && step.throw) {
const error: ValidationError = step.throw();
error.rawValueToHuman = this.rawValueToHuman;
error.thingName = thingName;