Editor: ajax function, question about Error callback vs return: false

Editor: ajax function, question about Error callback vs return: false

rldean1rldean1 Posts: 141Questions: 66Answers: 1

Long story short, I'm processing data from SQL manually for Editor. I'm using JavaScript. Basically, I pass the success callback function as a parameter to another part of my application that receives/processes the data returned from SQL. Of course, I make sure I structure the data appropriately for Editor's success callback function.

/*
PSEUDO CODE !! :)
*/

//Editor:  Ajax configuration for form data submission. 
ajax: function (method, url, data, successCB, error) {

     //manipulate the form data here
     doTheThing(successCB);

};

//elsewhere in my application
doTheThing:  function (data, options) {

     successCB = options;
     jsonStructureFromServer = data;

     successCB(jsonStructureFromServer);

};

My question:

I know the expected structure for the success callback; however, what is the expected json structure for the error callback? What does the error callback function do?

Basically, my JavaScript app handles errors from SQL in a specific way. If data is submitted, and SQL kicks back an error, no JSON is returned for the success callback to act upon, and the opened form just hangs.

I assume I should use the error callback to handle errors from SQL? I'm pretty sure I can pass the error callback function to the area of my application that handles errors.

What's the best way I can tell Editor that an error occurred on the server, and it should just close the open form?

This question has an accepted answers - jump to answer

Answers

  • rldean1rldean1 Posts: 141Questions: 66Answers: 1

    OK, so I may be making this more complicated than necessary, but I look forward to your responses anyway.

    I've discovered that the success callback function can process "errors" too.

    So, basically, in the part of my application that receives/processes the erorr, I create an object with an error string, and run the success callback against it:

                    if (options !== null && typeof options === 'function') {
    
                        successCB = options;
    
                        obj = {};
                        obj.error = message; //the error message
                        successCB(obj);
                        
    
                    }
    
    

    Atleast now the form window can close. It's not the sexiest, but it works. Unless y'all know a better way.

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin
    Answer ✓

    I know the expected structure for the success callback; however, what is the expected json structure for the error callback? What does the error callback function do?

    There is an important distinction to make between what the success and error functions do. Call success whenever the processing on the server is complete - including validation errors!

    The parameters you can send back for errors are documented here.

    So success is for correctly handled actions on the server. error is for when it all breaks down - uncaught exceptions, invalid JSON that kind of thing. In theory it should never be called :).

    Allan

This discussion has been closed.