extensions/String: use custom exception type

This commit is contained in:
Rory&
2026-06-17 01:48:23 +02:00
parent 388a705273
commit 4c25f84431
2 changed files with 31 additions and 12 deletions
+13 -1
View File
@@ -18,12 +18,24 @@
import { NextFunction, Request, Response } from "express";
import { HTTPError } from "lambert-server/HTTPError";
import { ApiError, FieldError } from "@spacebar/util";
import { ApiError, FieldError, FieldErrors } from "@spacebar/util";
import { StringLengthOutOfBoundsException } from "@spacebar/extensions";
const EntityNotFoundErrorRegex = /"(\w+)"/;
export function ErrorHandler(error: Error & { type?: string }, req: Request, res: Response, next: NextFunction) {
if (!error) return next();
// Convert custom generic exception classes to spacebar errors
if (error instanceof StringLengthOutOfBoundsException)
error = FieldErrors({
[error.key]: {
code: "BASE_TYPE_BAD_LENGTH",
message: req.t("common:field.BASE_TYPE_BAD_LENGTH", {
length: `${error.min} - ${error.max}`,
}),
},
});
try {
let code = 400;
let httpcode = code;