• If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Announcement

Collapse
No announcement yet.

[HELPED] Effecient Way To Globally Handle Ajax Error Responses

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [HELPED] Effecient Way To Globally Handle Ajax Error Responses

    So, I implemented a mechanism on my back end programs to globally monitor for any errors (data decimal, bad dates, etc). I set up a global MONITOR and ON-ERROR I use vvOut_toJsonPair to send back varying SUCCESS responses. Here are some examples of the response messages:

    Code:
    vvOut_toJsonPair('SUCCESS:99,MSG:System is currently down');
    
    ...
    
    vvOut_toJsonPair('SUCCESS:PGMERR,MSG:<QMHRCVPM message>');
    This sounds great so far, but now I need to look for those responses in the front-end code. So, my front end apps typically use a grid, with a tool bar that contains filters for the grid. Each filter uses a data store that is populated from a "proxy" ajax request. So if I have a grid (that uses a store), and 5 filters for the grid (that all use stores), that means I have to monitor for the above messages in 6 different places in the code. On top of that, I created a custom function that makes a standard ajax request to update records from the grid to the database. This "save" function will determine which form field is in error (if any) and will markInvalid that field.


    The solution to me is to have 2 different custom functions built to handle ajax responses: a proxy function and a singleton function. Is there a more fluid way to handle this?
    Last edited by rkanemeier; 09-12-2013, 10:41 AM.

  • #2
    After my 2nd cup of coffee the solution presented itself (fairly simple one on top of it).

    All I needed to do was create a generic function that receives the response text and processes the "global" errors (whatever that may be). In the case of the ajax singleton for my form submission, I am already checking for a SUCCESS = 1 and SUCCESS = 0. I just needed to add one more "else" condition at the end to capture "all other errors".

    I guess I was (and still am) wondering how everyone else is handling error responses that are not form related.

    Comment


    • #3
      You can override the global class of stores, proxy or ajax request to have a default function on failure.

      Like you noticed, you can reference a status and/or message in your failures.

      And finally, I would pop up an Ext Message Window with the Message if the error was not form related. If it is grid related, you can also highlight or bold a row.

      Comment


      • #4
        All network activity goes through Ext.data.Connection and this object has a "requestexception" event that you can listen for.

        Comment


        • #5
          Eric,

          I am under the impression that an ajax failure will not fire if the back end program returns an "appropriate" response. I will be sending back an appropriate response, albeit a response with error information. What am I missing with your idea?

          Sean,

          After looking at the docs, doesn't the requestexception fire only if the server sends back an error code? (ergo it's not a back-end soft-coded json error response.

          It's very possible I'm not seeing something properly.

          Comment


          • #6
            From the docs on requestexception:

            Fires if an error HTTP status was returned from the server. This event may also be listened to in the event that a request has timed out or has been aborted.


            requestexception will work for all exceptions (bad response, no response, etc..)

            Comment


            • #7
              I might see the disconnect here.

              You want to respond to 'failure' via your codes, when the ajax succeeds, but you are returning an error message.

              So, it sounds like you had actually solved your problem below. Does my advice of a message window for non-form related errors help you?


              I am already checking for a SUCCESS = 1 and SUCCESS = 0. I just needed to add one more "else" condition at the end to capture "all other errors".

              Comment


              • #8
                Eric,

                You are correct! I don't want our RPG programs sitting in MSGW in the QHTTPSVR subsystem. So I am using a MONITOR statement that encompasses all of our "mainline" code. ON-ERROR, I am building an AJAX response that sends the error message back with a different SUCCESS code (success = 0 means no error, all other success values means an error occurred).

                As far as a message window, I've bee using the Ext.msg.alert component. Is that the window you are referring to?

                Sean,

                Are you suggesting that I can manually send back some sort of HTTP error code using vvOut_jsonToPair?

                Comment


                • #9
                  Ext.Msg.alert is a method which takes two strings and then builds you an Ext.window.MessageBox with some default items set.

                  http://docs.sencha.com/extjs/4.0.7/#...x-method-alert

                  The Ext.window.MessageBox happens to be an extension of Ext.window.Window itself.

                  It's kind of your lucky day because I wrote similar code very recently. Based on this example below, you'll see I'm building html and making it the body of the window. So per Sean's comment, you could build a string on the server, and insert that in your window body.

                  You could even do still use the Ext.Msg.alert() but you have more options using Ext.Msg.show() or creating a window per below.

                  Code:
                  showErrorWindow : function(er) {
                      var errorHtml = '';
                  
                      //for each error in the er array, add to the html string
                      Ext.each(er, function(erInfo) {
                          errorHtml += '<div style="padding:10"><b>' + erInfo.type + ':</b> ' + erInfo.msg + '</div>';
                          
                      });
                      
                      
                      var w = Ext.create('Ext.window.Window', {
                          title: 'Error with Definition',
                          height: 150 + er.length * 30,
                          width: 600,
                          layout: 'fit',
                          scope: this,
                          modal : true,
                          html: errorHtml,
                          buttonAlign: 'center',
                          buttons: [{
                              text: 'OK',
                              scope: this,
                              handler: function() {
                                  //I removed code from here
                                  //The code took action based on the first error in the list.
                                  w.close();
                              }
                          }]
                      });
                      w.show();
                  },

                  Comment

                  Working...
                  X