Using server side response codes

I struggled with server side response codes. I had a form and it’s server action I had a query, if that query returned 0 results, i needed the submit to return an error.

Using google, most of what I found were people asking my question about the “response” properties and answers just being “It’s not in json format”. These weren’t helpful. I couldn’t find references for it in documentation.

Realizing the response was setup to return html response codes (like 403, 404, 200, etc). I finally found this community forum entry where it lined up response codes to wappler: HTTP Status Code to app connect name mappings

Here is the MDN reference page for response codes: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses

I wanted to trigger on an error, so my response properties looks like this:
image

On my form, I add the dynamic event from server connect, selected Error and use a the browser function to display an alert. My only issue remaining is this alert will generate any time there is an error and not just the one I added for response.
image

If you’re struggling with using the response and are new to programming I hope this helps.

Do you actually need to return an error status (such as code 402), or all you need to do is to is to show an alert when no records are returned from your database query?

The form submit server action is ultimately an update to a record for a ticket. In that I have a conditional that checks if they’re changing the status to closed, meaning resolved.

If they are changing the status to closed, the sub query inside the server action returns records for work logs. If there hasn’t been an update to the ticket, in the form of a work logs, the length is 0, it needs to return an error and not submit until they add the notes. This keeps us from closing tickets without adding documentation of what was done or not done.

Ok so can you explain a bit more then?
You say:

What’s the issue with that? On error you just show the specific error message in the alert - isn’t that what you want?

It took me a minute to figure out how to use the response function. So once I got that exact condition working, changing the status to canceled without entries in the work log, i went to bed. I will look at it further.

However if there is a legit issue with submitting that status change for the ticket and you do have the proper entries yet an error occurs it will tell the end user they need to add work logs because i’m alerting based on there being an error. On my project plan I have it on there to revist how to handle that better next week. I’m sure there are options to filter for that specific alert.

Now I’m moving over to creating the external piece, a dashboard for our clients to see metrics on everything we do for them, plus submit a ticket. This will be a separate project that has hooks into the current project our internal team uses.

Sorry for getting in your conversation but I think it would be easier to insert a validation for your status (and automatically stops the form from submitting and inform the user of what is wrong) instead of using response?
Just saying…

are you talking about this validation? or required fields validation?

1 Like

Yes, sorry for not typing it right…
Not stop the form from sybmitting, just stop it before going the sub query stuff and your server action fails

it might have been… since i’m not sure which validation you mean.

I didn’t see a way with required fields to accomplish that , unless I’m controlling the submit action based on criteria (subquery) from the client side, which I don’t like doing.

The data validation on the server side, I didn’t see how to exactly accomplish what I needed (check to esnure the child records existed before allowing the update statement) so I moved on to response. Which I made work like I need it too and it’s all server side.

I don’t know what is your serverAction flow…

I saw this on your post and perhaps checking this out…
If you focus on that I beleive you’ll find the way or please share a screenshot of your serveraction and point us the step that the problem occurs

I wasn’t asking for help. The original post was to help those like me who want to use the response function but can’t find a clear way to do use it.

The comment you quoted, that’s talking about error trapping the submit, it’s something i’ll look more into later is all I am saying. The way it is setup right now, if you have a legit error on submitting this form, it would still give the end user an alert to add an entry to the ticket for work done and that may not be the issue.

We’re a small team so this shouldn’t be a possibility and if it is, the feed back loop is pretty short. They can turn around and tell me. I’m simply acknowledging it could be an issue in the future.