How to display an error message generated from MySQL

Hi, I have created a page for Signon that is based on a User table in MySQL. One of the fields is Email. In the underlying MySQL there is a unique index defined for the Email column of the User table.

When I create a User in the page with a new email, it creates perfectly. When I try again with the same email (which I do deliberately to check that the error will be caught), I get a 500 notification, but the text of the error reason for the 500 is not displayed.

In Inspect in Chrome, the problem is clearly indicated.

How do I get the MySQL error to be displayed in an Alert or some such, so that the user has a clue as to what to do about the error that has been caused.

I realize that this error is easily trapped with a Exists validation, so this question is just used as an example of how to handle unanticipated errors from MySQL.

You can use the “error” dynamic event of server connect and display tge last error.

If you need to do more error handling server side you can wrap the failing database action in a try/catch action.

Hi
I could not find any tutorials on try/catch, must be me, they gotta be there somewhere.

I believe it is the ‘insert’ step that generates the error when there is an attempt to add a duplicate,

Anyway here us my server connect routine. Where do I add in the “error” dynamic event and how do I get the error into a client side Alert? (if that is how it is done)

btw, I modified the above server connect logic to…

image

This should still work?

  • Put the try/catch action in the steps.
  • Add the DB insert in the try step and maybe add some set values underneath if you wish.
  • Add a set value with dynamic $_ERROR value in the catch step and toggle output.

@dbatesmdbctech gave an example here

Hi TimD Most helpful. You have stored the error message into the database, which is useful for a batch process like your example, but in an online context how do you display the error to the user in a timely manner? Fred refers to a dynamic $_ERROR, but I am not sure how to use it to ‘toggle output’. What do you think that means?

Something along this:
image

Hi Fred, I am following your hints, in in my server connect i have…

image

When I do this the 500 error disappears from my form when I save, in fact the form achieves a status if ‘Successful’, even though I know the insert in the above flow has not worked (because I have deberately made it try to save a duplicate email)

So the question remains, how do I manage the error variable above to be displayed to the user.

I tried a strategy of creating a error variable in the Input POSTs and using that in an Alert in the Success event of the form, but that did not work.

Sorry I did not quite get your original issue. I can suggest this instead, but that means you create the error message from scratch, you don’t fetch the SQL message:

I’m no expert, just trying to share my limited knowledge when it feels right.

Hi Fred, no problem, its a journey alright. I really like the discussion and teh swapping of ideas.

Anyway, the original question was to trap unexpected errors. I did have the validate originally, such as your last suggestion, that is great for trapping the expected error of avoiding duplicates.

Anyway, the good news is that I did find the answer. It is to force an invalid status back to the form then all the error variables liven up with something useful. Check this out.

Then on your form you can set up a notification or an alert with…

image

It needs tidying up, but the essentials are there.

1 Like

Another old thread which might be/have been useful is here.

1 Like

Yep, I did see that one and it had a couple of clues. Along with more clues from you and Fred it has been a happy ending. Now I just have to put this answer thru all my pages.

Thank you kindly for your help and involvement.

2 Likes