generated from muhtadeetaron/nextjs-template
fix(api): fix api endpoint logic #3
This commit is contained in:
@ -12,9 +12,17 @@ import FormField from "@/components/FormField";
|
||||
import DestructibleAlert from "@/components/DestructibleAlert";
|
||||
import { RegisterForm } from "@/types/auth";
|
||||
|
||||
interface ValidationErrorItem {
|
||||
type: string;
|
||||
loc: string[];
|
||||
msg: string;
|
||||
input?: unknown;
|
||||
ctx?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
interface CustomError extends Error {
|
||||
response?: {
|
||||
detail?: string;
|
||||
detail?: string | ValidationErrorItem;
|
||||
};
|
||||
}
|
||||
|
||||
@ -36,16 +44,27 @@ export default function RegisterPage() {
|
||||
});
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const handleError = (error: { detail: string }) => {
|
||||
if (error?.detail) {
|
||||
const match = error.detail.match(/Key \((.*?)\)=\((.*?)\)/);
|
||||
if (match) {
|
||||
const field = match[1];
|
||||
return `The ${field} already exists. Please try again.`;
|
||||
function formatError(error: unknown): string {
|
||||
if (error && typeof error === "object" && "response" in (error as any)) {
|
||||
const customError = error as CustomError;
|
||||
const detail = customError.response?.detail;
|
||||
|
||||
if (typeof detail === "string") {
|
||||
return detail; // plain backend error string
|
||||
}
|
||||
|
||||
if (Array.isArray(detail)) {
|
||||
// Pick the first validation error, or join them if multiple
|
||||
return detail.map((d) => d.msg).join("; ");
|
||||
}
|
||||
}
|
||||
return "An unexpected error occurred. Please try again.";
|
||||
};
|
||||
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
|
||||
return "An unexpected error occurred.";
|
||||
}
|
||||
|
||||
const validateForm = () => {
|
||||
const { ssc_roll, hsc_roll, password } = form;
|
||||
@ -69,31 +88,12 @@ export default function RegisterPage() {
|
||||
|
||||
try {
|
||||
await register(form, setToken);
|
||||
router.push("/home");
|
||||
} catch (error) {
|
||||
// Type guard for built-in Error type
|
||||
if (error instanceof Error) {
|
||||
console.error(
|
||||
"Error:",
|
||||
(error as CustomError).response || error.message
|
||||
);
|
||||
|
||||
const response = (error as CustomError).response;
|
||||
|
||||
if (response?.detail) {
|
||||
const decodedError = handleError({ detail: response.detail });
|
||||
setError(decodedError);
|
||||
} else {
|
||||
setError(error.message || "An unexpected error occurred.");
|
||||
}
|
||||
} else {
|
||||
// Fallback for non-standard errors
|
||||
console.error("Unexpected error:", error);
|
||||
setError("An unexpected error occurred.");
|
||||
}
|
||||
router.push("/login");
|
||||
} catch (err: unknown) {
|
||||
setError(formatError(err));
|
||||
console.error("User creation error: ", err);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<BackgroundWrapper>
|
||||
<div className="min-h-screen flex flex-col items-center justify-center px-4 py-10">
|
||||
|
||||
Reference in New Issue
Block a user