Showing error ( {message: ..., digest: ...} ) instead of showing error message.
Unanswered
Muawiyah posted this in #help-forum
MuawiyahOP
Showing error ( {message: ..., digest: ...} ) instead of showing error message.
My Codes:
api/categories.ts
add-category-form.tsx
My Codes:
api/categories.ts
// Add a category
export const addCategory = async (data: addCategoryData) => {
const token = cookies().get("token")?.value;
const options = {
headers: {
Authorization: `Bearer ${token}`,
},
};
const url = `${apiUrl + route}`;
try {
const response = await axios.post(url, data, options);
return response.data;
} catch (error: any) {
throw error.response.data;
}
};add-category-form.tsx
const handleSave = async () => {
try {
const data = {
name,
slug,
image: images[0],
metaTitle,
metaDescription,
};
const response = await addCategory(data);
toast.success(response.message);
} catch (error: any) {
toast.error(error.message);
}
};11 Replies
@Muawiyah it shows the message.. so I don't see the issue
@Muawiyah Showing error ( {message: ..., digest: ...} ) instead of showing error message.
My Codes:
api/categories.ts
// Add a category
export const addCategory = async (data: addCategoryData) => {
const token = cookies().get("token")?.value;
const options = {
headers: {
Authorization: `Bearer ${token}`,
},
};
const url = `${apiUrl + route}`;
try {
const response = await axios.post(url, data, options);
return response.data;
} catch (error: any) {
throw error.response.data;
}
};
add-category-form.tsx
const handleSave = async () => {
try {
const data = {
name,
slug,
image: images[0],
metaTitle,
metaDescription,
};
const response = await addCategory(data);
toast.success(response.message);
} catch (error: any) {
toast.error(error.message);
}
};
Server actions should not throw. If a server action throws, nextjs treats it as “developer fked up” rather than “user fked up”, and return 500 + a generic error message for security reasons. For expected errors (“user fked up”), return a value indicating that something went wrong, like
return { success: false, message: "…" }@joulev Server actions should not throw. If a server action throws, nextjs treats it as “developer fked up” rather than “user fked up”, and return 500 + a generic error message for security reasons. For expected errors (“user fked up”), return a value indicating that something went wrong, like `return { success: false, message: "…" }`
MuawiyahOP
But I used the same way for other api endpoints in that same project, and that register and login user worked perfectly
MuawiyahOP
Same thing
@joulev if I use return. Then try catch will not work perfectly
@Muawiyah <@484037068239142956> if I use return. Then try catch will not work perfectly
If you return a value instead of throwing it, you need to get that value normally instead of try/catching it
In the code above, you won’t ever get to the toast.error branch because the action doesn’t throw. You need to manually read the response value and decide if you want to use toast.success or toast.error