How can i read backend error message?
Unanswered
Entlebucher Mountain Dog posted this in #help-forum
Entlebucher Mountain DogOP
when i'm getting status 500 or 400 didn't get error message in the catch
what's wrong with my code ?
"use server";
import { api_url } from "@/config";
import { revalidateTag } from "next/cache";
export const addCategory = async (newCategory: FormData) => {
try {
const res = await fetch(
method: "POST",
body: newCategory,
});
revalidateTag("category");
return {
status: res.status,
data: null,
};
} catch (error) {
console.log(error);
}
};
what's wrong with my code ?
"use server";
import { api_url } from "@/config";
import { revalidateTag } from "next/cache";
export const addCategory = async (newCategory: FormData) => {
try {
const res = await fetch(
${api_url}/category/, {method: "POST",
body: newCategory,
});
revalidateTag("category");
return {
status: res.status,
data: null,
};
} catch (error) {
console.log(error);
}
};
12 Replies
Toyger
you have runtime logs at vercel https://vercel.com/docs/observability/runtime-logs
You can do this
if (!res.ok) {
throw new Error('Failed to fetch data')
}
if (!res.ok) {
throw new Error('Failed to fetch data')
}
@Flávio inácio You can do this
if (!res.ok) {
throw new Error('Failed to fetch data')
}
Entlebucher Mountain DogOP
if i do that can i see the error that is sent from backend ?
You can eliminate this try/catch and using only
const res = await fetch(${api_url}/category/, {
method: "POST",
body: newCategory,
});
const json = await res.json();
if (!res.ok) {
console.error(json);
return;
}
revalidateTag("category");
return {
status: res.status,
data: null,
};I'm not sure the .json() will work if res.ok is false?? (lack of body)
const res = await fetch(`${ api_url }/category/`, {
method: "POST",
body: newCategory,
});
if (!res.ok) {
res.status(500).json({ error: res.statusText });
}
const json = await res.json();
revalidateTag("category");
return json;@goodsie I'm not sure the .json() will work if res.ok is false?? (lack of body)
js
const res = await fetch(`${ api_url }/category/`, {
method: "POST",
body: newCategory,
});
if (!res.ok) {
res.status(500).json({ error: res.statusText });
}
const json = await res.json();
revalidateTag("category");
return json;
If you don't need the backend error message from the backend, then it's ok. Otherwise, you need "const json = await res.json();", before the if, to get the error message returning through the back
I wasnt aware that .json() would even have value if !ok ?
It is important to remember that Next Js made an extension to the web fetch API
odd.
When I make a fetch that does not succeed I get:
from trying to use res.json() when !res.ok
When I make a fetch that does not succeed I get:
Uncaught SyntaxError SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSONfrom trying to use res.json() when !res.ok
@goodsie odd.
When I make a fetch that does not succeed I get:
Uncaught SyntaxError SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
from trying to use res.json() when !res.ok
It will depend on the back-end response format when an exception occurs
In this case