Next.js Discord

Discord Forum

Error handling in server actions

Answered
West African Lion posted this in #help-forum
Open in Discord
Avatar
West African LionOP
I have a function that fetches data, in case of an error I return an error message, else I return the data.
In the component that's calling the server action, how can I know if the function returned an error message or data?
Image
Answered by B33fb0n3
you can check if the response is a string. If it's a string, it's the error. If it's not the string, it's the data.

Keep in mind, that you are also able to call fetch directly inside your component
View full answer

14 Replies

Avatar
you can check if the response is a string. If it's a string, it's the error. If it's not the string, it's the data.

Keep in mind, that you are also able to call fetch directly inside your component
Answer
Avatar
West African LionOP
I thought about it .. but I thought there would be some better way of doing it? Is there?
Avatar
yea, when you call the fetch inside your component directly you can also access other data. Of course you can also return this ok value in your error as well to make it available to your frontend
Avatar
West African LionOP
by calling the fetch directly in my component you mean not creating a seperate function? so that I can the error directly?
Avatar
when you executing the fetch directly inside your component you can either create a function for it or only call it. The function could look like this:
const getTransactions = () => {
  return fetch("/your/endpoint.php")
}
Avatar
West African LionOP
ohhh I see .. but is this better than this option?
Avatar
I can only tell you what kind of options you have. At the end you need to decide which one you would like to use. You are the only person that knows your project best
Avatar
West African LionOP
fair enough, but since I'm a beginner at this, I much prefer to know the opinion of more experienced developers like yourself.
I appreciate the feedback, thank you.
Avatar
if I would need to choose between the following options:
1. passing the ok to the response of the server action
2. using the fetch directly inside the component

I would choose 2. because then I have all fields that I might need to access and I don't need to wrap a serverside request within a serverside request when it's a public api. So instead of having
Client -> Server -> Server
I prefer
Client -> Server
Avatar
West African LionOP
very interesting, thank you for the explanation. Have a good day.
Avatar
West African LionOP
what makes this case with public API's different than other cases where you may use client->server->server?
Avatar
imagine you writing an app and a seperate backend. This backend is only accessable with a secret token. But you want, that only your app is able to make request again the backend. So your app (nextjs app serverside) gets the secret and makes requests again your api.

Client -> Server (with private secret) -> Backend Server (checks secret)
happy to help
Avatar
West African LionOP
Ohhhhh I see .. I see .. this is certainly what I wanna do .. I haven't set any secret keys just yet
But I really appreciate taking the time to explain again
Thank you