Next.js Discord

Discord Forum

Showing error ( {message: ..., digest: ...} ) instead of showing error message.

Unanswered
Muawiyah posted this in #help-forum
Open in Discord
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);
    }
  };

11 Replies

but api returning like this
in terminal showing like this
Please ping me when you reply
@Muawiyah it shows the message.. so I don't see the issue
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