How to handle a SQL error response in Editor

How to handle a SQL error response in Editor

BoSmithBoSmith Posts: 23Questions: 6Answers: 0

Hello,

This is really not a problem but a how to question:

If I have a table that has a field that is a foreign key in another table, SQL responses properly with an error message stating that the row cannot be deleted when the user tries to delete the row in the Editor.

My question is how to best to handle the error in the Editor and tell the user that this row cannot be deleted. Right now it just shows the SQL error string in red at the bottom of the Editor popup.

This is the response that I would like to handle better:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails ...

Thanks

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,836Questions: 1Answers: 10,518 Site admin
    Answer ✓

    There are a couple of options:

    1. Use ON DELETE CASCADE so the child row will be deleted
    2. Use a server-side event handler to delete the child row.
    3. Use postSubmit to detect the error message returned from the server and modify it to be something more using friendly.

    From your description I suspect that 3. will be the one you want. It isn't ideal I realise! 1.6 is going to introduce cancellable server-side events, so you can effectively do validation prior to a delete, which will be a much neater way of doing this.

    Allan

  • BoSmithBoSmith Posts: 23Questions: 6Answers: 0

    Thanks Allan for the suggestion. I look forward to 1.6 also.

  • rf1234rf1234 Posts: 3,028Questions: 88Answers: 422

    That was really helpful! Here is an example for custom error handling using "postSubmit". Maybe of use for someone who finds this post.

    Editor.on( 'postSubmit', function ( e, json, data, action ) {
                    if (json.error) {
                        var sqlDupKey = json.error;
                        sqlDupKey = sqlDupKey.substring(0, 15 )
                        if (sqlDupKey == 'SQLSTATE[23000]') {
                            json.error = 'Sorry, you already have an assigned role!';
                        }
                    }        
        });
    
This discussion has been closed.