Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Where to trap Store / Proxy exception?

  1. #11
    Sencha User
    Join Date
    Dec 2007
    Posts
    43

    Default Overriding Ext.data.Reader for results missing the root

    I was having a similar issue with the following error being thrown when the response does not include the defined root:

    Uncaught TypeError: Cannot read property 'length' of undefined

    I'm using the following override to get around the error, of course YMMV

    Code:
    Ext.override(Ext.data.Reader, {
        readRecords: function(data){
            this.rawData = data;
            
            data = this.getData(data);
            
            var root = this.getRoot(data);
            //BEGIN OVERRIDE - insert empty root if one does not exist
            if (root == undefined) {
                var rootName = this.root;
                data[rootName.toString()] = new Array();
                root = this.getRoot(data);
            }
            //END OVERRIDE 
            var total = root.length, success = true, value, records, recordCount;
            
            if (this.totalProperty) {
                value = parseInt(this.getTotal(data), 10);
                if (!isNaN(value)) {
                    total = value;
                }
            }
            
            if (this.successProperty) {
                value = this.getSuccess(data);
                if (value === false || value === 'false') {
                    success = false;
                }
            }
            
            records = this.extractData(root, true);
            recordCount = records.length;
            
            return new Ext.data.ResultSet({
                total: total || recordCount,
                count: recordCount,
                records: records,
                success: success
            });
        }
    });

  2. #12
    Sencha User Mis63's Avatar
    Join Date
    Aug 2010
    Location
    Clermont-Ferrand, France
    Posts
    42

    Default

    I am having the same problem.
    The success property of JSON response.

    Can Sencha team tell us more about this : how to detect error sent in JSON response ?
    Is a fix planned ?

  3. #13
    Sencha User bclinton's Avatar
    Join Date
    Nov 2007
    Posts
    305

    Default

    Quote Originally Posted by Mis63 View Post
    I am having the same problem.
    The success property of JSON response.

    Can Sencha team tell us more about this : how to detect error sent in JSON response ?
    Is a fix planned ?
    I don't know if this was the best place to put it, but I was able to check the success property in the readRecords method before the superclass.readRecords was called in an override like this:

    (code added to the original method in red)

    Code:
    Ext.override(Ext.data.JsonReader, {
        readRecords: function(data) {
            //this has to be before the call to super because we use the meta data in the superclass readRecords
            if (data.metaData) {
                this.onMetaChange(data.metaData);
            }
    
           if (!data.success)
           {
                Ext.Msg.alert('Error', 'Success was false', Ext.emptyFn);
            }
    
            /**
             * DEPRECATED - will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead
             * @property jsonData
             * @type Mixed
             */
            this.jsonData = data;
    
            return Ext.data.JsonReader.superclass.readRecords.call(this, data);
        }
    });
    I originally bypassed the "return Ext.data.JsonReader.superclass.readRecords.call(this, data);" line when success was false, but this gave me the "Uncaught TypeError: Cannot read property 'length' of undefined" error in the store.

    To avoid this error I make sure I add an empty array for the store's root (in my case "rows") so my json looks like this:
    Code:
    {
        "success":false,
        "msg":"Your session has expired. You will be redirected to the login page.",
        "rows":[]
    }

  4. #14
    Sencha User interfasys's Avatar
    Join Date
    Mar 2011
    Location
    UK & Switzerland
    Posts
    125

    Default

    None of these solutions will catch syntax errors thrown when the JSON object has a big problem. Does Sencha Touch provide a way to catch these other than using the window object?

  5. #15
    Ext JS Premium Member
    Join Date
    Oct 2010
    Posts
    68

    Default

    In response to @danderson's override. It works great, and I intend to keep it around until that time if/when Sencha changes the reader to handle these situations better.

  6. #16
    Sencha User
    Join Date
    Sep 2011
    Location
    Zrich, Switzerland
    Posts
    19

    Default

    hi

    you can do the following to catch any "success: false":

    Code:
    this.store.load({
                        scope   : this,
                        callback: function(records, operation, success) {
                            //the operation object contains all of the details of the load operation
                            if(operation.resultSet.success){
                                this.index();
                            } else {
                                Ext.Msg.alert('Failure','there was a failure: success is false');
                            }
                        }
                    });
    cheers
    katam

Page 2 of 2 FirstFirst 12

Similar Threads

  1. [FIXED-334] Proxy Event 'exception' Never Firing
    By Shane F in forum Sencha Touch 1.x: Bugs
    Replies: 5
    Last Post: 28 Sep 2010, 6:22 PM
  2. Change Store Proxy URL without Creating New Store
    By rkdeiss in forum Ext 3.x: Help & Discussion
    Replies: 1
    Last Post: 13 Apr 2010, 11:16 AM
  3. Store proxy exception fired though successful
    By augustinas in forum Ext 3.x: Help & Discussion
    Replies: 8
    Last Post: 7 May 2009, 3:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •