Using the error from server actions in client
Unanswered
Spectacled bear posted this in #help-forum
Spectacled bearOP
const submitFunction = async (data: TFormSchema) => {
try {
const response = await passwordLoginAction(data);
router.push("/admin/dashboard");
} catch (e) {
console.log(e);
const error = e as AxiosError<TLoginErrorResponse>;
form.setError("root", {
type: "manual",
message: error.response?.data.errors[0],
});
}
};"use server";
import axios from "axios";
import { TFormSchema } from "./form-schema";
import { LOGIN_URL } from "../../../../../api-routes";
import { cookies } from "next/headers";
export async function passwordLoginAction(data: TFormSchema) {
try {
const response = await axios.post(LOGIN_URL, data);
cookies().set("Authorization", response.data.token.access, {
httpOnly: true,
secure: true,
});
cookies().set("Refresh", response.data.token.refresh, {
httpOnly: true,
secure: true,
});
return true;
} catch (error) {
return error;
}
}This code is giving me RangeError: Maximum call stack size exceeded.
If I throw the error instead of returning in the server action, I'm getting a axios error 401, which I have no idea where it coming from. Coz my api will give status 400 only on error