How to display text of response?

Good afternoon.

In server actions, I have created an object “Response” with Status 501 and Text “Error email”.
On the client side I run this server action on a form and if error occurs I would like to display that text (“Error email”).
But when error occurs, I always get “Internal server Error”.

Server Action:

Client Side:

Best Regards.

Well 5xx HTTP status codes are server errors, so you don’t really want to use these for your response step.
You can learn more here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

What exactly are you trying to achieve?

I would like to notify the user that his registration has not been possible.
How do you recommend doing it?

What condition do you have and what do you check? In what case should the status be set?

I have tried Status 999, but when I access “lastError.message”, I get 999, instead of the message.

Server Action:

Client Side:

image

image

Best Regards.

You can't just use a random status, it needs to correspond to what you are doing, so:

Also, if you don't want to see standard alerts, just don't use the standard alert messages, use the toast or notify components instead.

I am trying to notify the user that his email already exists in the system.
The check works perfectly for me with a SQL query.
But I don’t know any other way to notify the client from the server other than through “Response”.

What is the “Response.Text” attribute for if it cannot be accessed from the client?

You can use the validation step for this in the server action. It will return a validation error on the front end which you can use to trigger an alert/notify.
It should also work the way you set it up, just return a 400 error code. Then you can access the response text on the clienside and show it in an alert or notify.
You can explore the response of the server action in the developer tools in your browser under Network > HXR

You mean “Validate Data”?

For now I have solved it with response 599 and showing “lastError.response” in an Alert. This allows me to access the “Response.Text” attribute.
If I use response 400, the “Error” event is not fired.

Thank you.

You need to use the Invalid dynamic event for your form for the 400 status in order to trigger an action for it (not the Error one).

Okay. Thanks.

You have the power of knowledge.

2 Likes

if you work with form and server action lasterror does.t work. You can see status, but not any message from your response.text (response code 200). But if you use ‘data’ variable instead of ‘status’ you can see this text and do some notifications to user.