Handle invalid error in the request form

Signed-off-by: Émile Ré <nemile.re@gmail.com>
This commit is contained in:
Émile Ré
2025-12-15 12:43:09 +01:00
parent 64d2ecee59
commit 893617ef51
5 changed files with 60 additions and 6 deletions

View File

@@ -58,6 +58,18 @@ export class ForbiddenError extends Error {
}
}
export class InvalidError extends Error {
field?: string;
cause?: string;
constructor(message?: string, field?: string, cause?: string) {
super(message || "INVALID");
this.name = "InvalidError";
this.field = field;
this.cause = cause;
}
}
export function buildEndpoint(path: string): string {
const host = import.meta.env.VITE_API_URL;
@@ -91,6 +103,9 @@ const hasUnauthorizedError = (error: GraphQLError) =>
const hasForbiddenError = (error: GraphQLError) =>
error.extensions?.code == "FORBIDDEN";
const hasInvalidError = (error: GraphQLError) =>
error.extensions?.code == "INVALID_REQUEST";
const fetchRelay: FetchFunction = async (
request,
variables,
@@ -183,6 +198,15 @@ const fetchRelay: FetchFunction = async (
if (forbiddenError) {
throw new ForbiddenError(forbiddenError.message);
}
const invalidError = errors.find(hasInvalidError);
if (invalidError) {
throw new InvalidError(
invalidError.message,
invalidError.extensions.field as string ?? "",
invalidError.extensions.cause as string ?? "",
);
}
}
return json;