Error-handling for server-side paging
Error-handling for server-side paging
Hi!
I am using DataTables 1.9.0 within a Microsoft MVC3 project. I am taking advantage of server-side pagination and, thanks to some great articles on the web, I got it working with no real issues! Nice!
The only thing I am struggling with is error-handling. What if the external source (i.e. the service identified by the sAjaxSource parameter, which, in my case, is a MVC Controller method) takes an error? I haven't seen any patterns for dealing with this. How can server-side errors be captured and a meaningful error returned to the client? In my testing, I'm simulating a case where the first page request successfully returns data, but the second page request encounters a server-side database error. I've tried returning an "empty" JSON object (i.e. a JSON object with just one row, containing error messages). I've also tried just letting the unhandled exception propagate out of my controller method. Neither worked properly. In both cases, the DataTable did not update itself. It just continued to display the first page of data with no indication that anything had gone wrong.
Can you point me to some examples of error-handling in this scenario? Ideally, I would like to be able to display a clean error message to my client letting them know that something went wrong.
Thanks!
Brian
p.s. I'm a relative newbie to client-side technologies like jQuery, so please bear with me if my question seems a bit obtuse :)
I am using DataTables 1.9.0 within a Microsoft MVC3 project. I am taking advantage of server-side pagination and, thanks to some great articles on the web, I got it working with no real issues! Nice!
The only thing I am struggling with is error-handling. What if the external source (i.e. the service identified by the sAjaxSource parameter, which, in my case, is a MVC Controller method) takes an error? I haven't seen any patterns for dealing with this. How can server-side errors be captured and a meaningful error returned to the client? In my testing, I'm simulating a case where the first page request successfully returns data, but the second page request encounters a server-side database error. I've tried returning an "empty" JSON object (i.e. a JSON object with just one row, containing error messages). I've also tried just letting the unhandled exception propagate out of my controller method. Neither worked properly. In both cases, the DataTable did not update itself. It just continued to display the first page of data with no indication that anything had gone wrong.
Can you point me to some examples of error-handling in this scenario? Ideally, I would like to be able to display a clean error message to my client letting them know that something went wrong.
Thanks!
Brian
p.s. I'm a relative newbie to client-side technologies like jQuery, so please bear with me if my question seems a bit obtuse :)
This discussion has been closed.
Replies
By default DataTables will bring up an alert if there is an error in the JSON parsing, based on what is returned (it can be changed to a 'throw' if you want), but that isn't brilliant for end users and not complete in the error handling since it silently ignores other errors.
If you want proper error control you need to specify an 'error' function in the jQuery Ajax handler - this is the default for DataTables: https://github.com/DataTables/DataTables/blob/master/media/src/model/model.defaults.js#L915 . So what to do is to have an fnServerData function with your own Ajax call that will correctly do what you want for your error handling.
Allan