0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

feat: basic validationError class

This commit is contained in:
bholmesdev 2024-04-23 14:34:26 -04:00
parent 86da5e7d29
commit 340c9d6101
2 changed files with 23 additions and 1 deletions

View file

@ -1,5 +1,6 @@
import type { APIContext } from 'astro';
import { AsyncLocalStorage } from 'node:async_hooks';
import type { ZodError } from 'zod';
export const ApiContextStorage = new AsyncLocalStorage<APIContext>();
@ -18,3 +19,18 @@ export async function getAction(pathKeys: string[]): Promise<Function> {
}
return actionLookup;
}
export class ActionError extends Error {
constructor(message: string) {
super(message);
}
}
export class ValidationError extends ActionError {
error: ZodError;
constructor(error: ZodError) {
super('Failed to validate');
this.error = error;
}
}

View file

@ -1,3 +1,5 @@
import { ValidationError } from './runtime/utils.js';
function toActionProxy(
actionCallback = {},
aggregatedPath = '/_actions/'
@ -21,7 +23,11 @@ function toActionProxy(
body,
headers,
});
return res.json();
const json = await res.json();
if (res.status === 400) {
throw new ValidationError(json);
}
return json;
}
action.toString = () => path;
// recurse to construct queries for nested object paths