update cuz presentation
This commit is contained in:
parent
7a9e451739
commit
4e67cc4e19
29 changed files with 1065 additions and 88 deletions
31
server/utils/validation.ts
Normal file
31
server/utils/validation.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
/* global createError */
|
||||
|
||||
export function createValidationError(errors: Map<string, string>) {
|
||||
let message = "Invalid parameters: ";
|
||||
for (const i in errors)
|
||||
message += i + ", ";
|
||||
message = message.slice(0, -2);
|
||||
return createError({
|
||||
statusCode: 400,
|
||||
message,
|
||||
data: {
|
||||
errors: Object.fromEntries(errors),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function handleRecursedValidationError(e: unknown, errors: Map<string, string>, element: string) {
|
||||
if (typeof e !== "object") throw e;
|
||||
if (!e) throw e;
|
||||
if (!(e as any).data || !(e as any).message) throw e;
|
||||
const upstreamErrors = (e as any).data.errors as any;
|
||||
const upstreamMessage = (e as any).message;
|
||||
if (upstreamErrors) {
|
||||
for (const j in upstreamErrors)
|
||||
errors.set(`${element}.${j}`, String(upstreamErrors[j]));
|
||||
} else if (upstreamMessage) {
|
||||
errors.set(`${element}`, String(upstreamMessage));
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue