tags:

views:

1683

answers:

5

I have a jqgrid that's functionning very well.

I was wondering is it possible to catch server sent errors ? how Is it done ?

+1  A: 

From what I see, it returns the data as a json string. So what you have to do is add an error handler that formats the error as a json string and prints it out as one. This can be done in php with the

set_error_handler

function.

The error handler would then I guess push the data in to jsonReturn.error, so you would just need to check for that when you are adding your data to the table.

If you are throwing exceptions instead of letting it get all the way to errors (probably a better practice), then you would want to format the exception as a json string.

Since it is returning the data in an xml format, you would want to parse the xml:

<xml>
    <error>
        error message
    </error>
</xml>

like this:

$(request.responseXML).find("error").each(function() {
        var error = $(this);
        //do something with the error
});

Shamelessly borrowed from: http://marcgrabanski.com/article/jquery-makes-parsing-xml-easy

SeanJA
No the server returns data in XML format! Any solution
ZeroCool
+5  A: 

I have recently made extensive use of jqgrid for a prototype project I am working on for CB Richard Ellis (my employer). There are many way to populate a jqgrid, as noted in the documentation: (see the "retrieving data" node).

Currently I make a service call that returns a json string that when evaluated, gives me an object that contains the following:

  • ColumnNames: string[]
  • ColumnModels: object[] (each object has the properties "name", "index" and "sortable")
  • Data: object[] (each object has properties that match the names in the column model)
  • TotalRows: int

In my success callback, I manually create the jqgrid like this: ("data" is the object I get when evaluating the returned json string).

var colNames = data.ColumnNames;
var colModel = data.ColumnModels;
var previewData = data.PreviewData;
var totalRows = data.TotalRows;
var sTargetDiv = userContext[0]; // the target div where I'll create my jqgrid

$("#" + sTargetDiv).html("<table cellpadding='0' cellspacing='0'></table>");
var table = $("#" + sTargetDiv + " > table");
table.jqGrid({
 datatype: 'local',
 colNames: colNames,
 colModel: colModel,
 caption: 'Data Preview',
 height: '100%',
 width: 850,
 shrinkToFit: false
});

for (var row = 0; row < previewData.length; ++row)
 table.addRowData(row, previewData[row]);

So you can see I manually populate the data. There is more than 1 kind of server error. There is the logical error, which you could return as a property in your json string, and check before you try to create a jqgrid (or on a per-row basis).

if (data.HasError) ...

Or on a per-row basis

for (var row = 0; row < previewData.length; ++row)
{
 if (previewData[row].HasError)
  // Handle error, display error in row, etc
  ...
 else
  table.addRowData(row, previewData[row]);
}

If your error is an unhandled exception on the server, then you'll probably want an error callback on your async call. In this case, your success callback that (presumably) is creating your jqgrid won't be called at all.

This, of course, applies to manually populating a jqgrid, which is only one of the many options available. If you have the jqgrid wired directly to a service call or a function to retrieve the data, then that's something else entirely.

On the documentation page, look under Basic Grids > Events. There you'll see the "loadError" event that might come in handy.

Samuel Meacham
hey sam... if you haven't already seen it.. http://skysanders.net/code/jQGridWS/default.aspxyou may be able to reuse some of it and maybe add some exception handling capabilities.
Sky Sanders
A: 

Use the callbacks. If you get an actual http error (a 400 or 500, for example), loadError(xhr, status, error) is triggered.

But some errors (like validation) shouldn't throw a 400 or 500 error. But you can still catch those in loadComplete(xhr). Parse your json, and check for whatever way you're using to identify errors. For example, I'm doing this in my loadComplete():

if (jsonResponse.errors) { $.each(jsonResponse.errors, function(i, val){ addErrorMessage($("#"+val.field), val.message); }); }

mcv
+1  A: 

If you look at the jqgrid demo site and look at "What's new in version 3.2" There should be a section about controlling server errors.

Specifically, it uses a callback parameter loadError:

loadError : function(xhr,st,err) { 
    jQuery("#rsperror").html("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);
}

As mcv states above, some errors are data errors, so you'll need to handle those specifically.

r00fus
A: 
function gridCroak(postdata, _url, grid, viewCallBack, debug) {
$(".loading").css("display", "block");
$.ajax({
    type: 'POST',
    url: _url,
    data: postdata,
    dataType: "xml",
    complete: function(xmldata, stat){
 if(stat == "success") {
     $(".loading").css("display", "none");
     var errorTag = xmldata.responseXML.getElementsByTagName("error_")[0];
     if (errorTag) {
  $("#ErrorDlg").html(errorTag.firstChild.nodeValue);
  $("#ErrorDlg").dialog('open');
     } else {
  var warningTag = xmldata.responseXML.getElementsByTagName("warning_")[0]; 
  if (warningTag) {
      $("#WarningDlg").html(warningTag.firstChild.nodeValue);
      $("#WarningDlg").dialog('open');
  } else {
      if (debug == true) {
   alert(xmldata.responseText);
      }
      jQuery(grid)[0].addXmlData(xmldata.responseXML);
      if(viewCallBack) viewCallBack();
  }
     }
 } else {
     $("#ErrorDlg").html("Servizio Attualmente Non Disponibile !");
     $("#ErrorDlg").dialog('open');
 }
    }
});
}

And In the Grid

datatype : function(postdata) { gridCroak(postdata, 'cgi-bin/dummy.pl?query=stock', 
                                                    "#list", null, false) },

At the end it does use the same approach I think.

Thanks all

ZeroCool