How to send a message back to DataTables if an error occurs server-side?

How to send a message back to DataTables if an error occurs server-side?

marcosdimitriomarcosdimitrio Posts: 8Questions: 2Answers: 0

When responding to a DataTables AJAX request, if an error happens server-side, I want to try/catch and send back a more descriptive message of the problem, so I can better inform the user instead of just showing the default message:

DataTables warning: table id=myTable - Ajax error. For more information about this error, please see http://datatables.net/tn/7

What's the proper way of doing this in DataTables? I have looked into the examples but it looks like all that DataTables reads is "draw", "recordsTotal", "recordsFiltered" and "data".

Answers

  • allanallan Posts: 61,657Questions: 1Answers: 10,094 Site admin

    Hi,

    You can tell DataTables what to do on error with the $.fn.dataTable.ext.errMode static property. There is some information about it available in the error documentation (the values it can take), but it really needs its own documentation page. I'll look into adding that!

    Allan

  • marcosdimitriomarcosdimitrio Posts: 8Questions: 2Answers: 0

    I had seen the error event documentation, it's triggered when the server gives an error, basically when things go out of control. I was looking for something to use when errors are caught by the server-side error handling routines.

    Perhaps DataTables could use something similar to JSON-RPC 2.0 response object. It uses either result or error to indicate success/failure. In DataTables, it could be the same, or to maintain backwards compatibility, it could be either "data" or "error".

    When successful, the server would respond:

    {
      "draw": 1,
      "recordsTotal": 57,
      "recordsFiltered": 57,
      "data": [
        ...
      ]
    }
    

    When unsuccessful, the response would be:

    {
      "draw": 1,
      "recordsTotal": null,
      "recordsFiltered": null,
      "error": [
        "code": -32601,
        "message": "Descriptive message about the error"
      ]
    }
    
  • allanallan Posts: 61,657Questions: 1Answers: 10,094 Site admin

    Oh - I see, sorry I misunderstood. If you send back an error parameter DataTables should show that.

    The $.fn.dataTable.ext.errMode could still be used with that (particularly is a non string error was returned) to do whatever display / update is needed on the client-side.

    Allan

This discussion has been closed.