Skip to content

Commit

Permalink
Handle pydantic spec errors (#9706)
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad authored Jan 3, 2025
1 parent c25c3c3 commit ed05fc0
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/Utils/request/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ export function handleHttpError(error: Error) {
}

if (isBadRequest(error)) {
Notifications.BadRequest({ errs: cause?.errors });
const errs = cause?.errors;
if (isPydanticError(errs)) {
handlePydanticErrors(errs);
return;
}
Notifications.BadRequest({ errs });
return;
}

Expand Down Expand Up @@ -62,3 +67,33 @@ function isBadRequest(error: HTTPError) {
function isNotFound(error: HTTPError) {
return error.status === 404;
}

type PydanticError = {
type: string;
loc: string[];
msg: string;
input: unknown;
url: string;
};

function isPydanticError(errors: unknown): errors is PydanticError[] {
return (
Array.isArray(errors) &&
errors.every(
(error) => typeof error === "object" && error !== null && "type" in error,
)
);
}

function handlePydanticErrors(errors: PydanticError[]) {
errors.map(({ type, loc, msg }) => {
const title = type
.replace("_", " ")
.replace(/\b\w/g, (char) => char.toUpperCase());

toast.error(`${title}: '${loc.join(".")}'`, {
description: msg,
duration: 8000,
});
});
}

0 comments on commit ed05fc0

Please sign in to comment.