Why doesn’t useMutation detect when server returns error?

Nothing happens onSuccess or onError. I only get

DELETE http://localhost:3000/api/facturas/46 net::ERR_ABORTED 500 (Internal Server Error)

  const { mutate: handleDelete } = useMutation({
    mutationFn: async () => {
      const response = await fetch(`/api/pagos-recibidos/${view}`, {
        headers: {
          access_token: access_token,
        },
        method: "DELETE",
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.message || "Failed to delete payment");
      }

      return response.json();
    },
    onSuccess: () => {
      const params = new URLSearchParams(searchParams?.toString());
      params.delete("view");
      router.push(`?${params.toString()}`);
      toast.success("Factura eliminada exitosamente");
    },
    onError: (error) => {
      console.error("Delete error:", error.message);
      toast.error(
        error.message || "An error occurred while deleting the payment",
      );
    },
  });