Error handling in server actions
Answered
West African Lion posted this in #help-forum
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?
In the component that's calling the server action, how can I know if the function returned an error message or data?
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
Keep in mind, that you are also able to call
fetch
directly inside your component14 Replies
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
Keep in mind, that you are also able to call
fetch
directly inside your componentAnswer
West African LionOP
I thought about it .. but I thought there would be some better way of doing it? Is there?
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 frontendWest African LionOP
by calling the fetch directly in my component you mean not creating a seperate function? so that I can the error directly?
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")
}
West African LionOP
ohhh I see .. but is this better than this option?
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
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.
I appreciate the feedback, thank you.
if I would need to choose between the following options:
1. passing the
2. using the
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
1. passing the
ok
to the response of the server action2. using the
fetch
directly inside the componentI 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
West African LionOP
very interesting, thank you for the explanation. Have a good day.
West African LionOP
what makes this case with public API's different than other cases where you may use client->server->server?
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)
Client -> Server (with private secret) -> Backend Server (checks secret)
happy to help
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
But I really appreciate taking the time to explain again
Thank you