Why does the error function option not work as described?

Why does the error function option not work as described?

FurburgerFurburger Posts: 37Questions: 8Answers: 0

I have a function called displayError() which displays errors in a nice modal.

If I set DataTables to use this function, like so . . .

$.fn.dataTable.ext.errMode = function ( message ) {
displayError( 'An error was encountered trying to create the Data Table: ' + message );
};

it still throws a message instead of calling my function.

I have looked at the source code and there is nothing there that I can see that supports the above functionality.

Any ideas?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,765Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Are you using the nightly version of DataTables? I think it is only available there until I release 1.10.5.

    Allan

  • FurburgerFurburger Posts: 37Questions: 8Answers: 0

    Works perfectly on the nightly build.

    Thanks Alan

  • FurburgerFurburger Posts: 37Questions: 8Answers: 0

    After testing it further, I have one suggestion:

    I you pass 3 parameters to the error function:

    (settings, help_page_number, message)

    Unfortunately you mangle the message with your own wording, e.g.

    "DataTables warning: table id=MattersBrowseTable - My error"

    which makes it hard for me to extract the message I actually raised, in this case "My error".

    I think it is unwise to presume that this is the wording I would want to show my users.

    Can you add a 4th parameter, maybe, which has just the error text, e.g. "My error" or preferably just return the unadulterated error message as the 3rd parameter.

    Your comments?

  • FurburgerFurburger Posts: 37Questions: 8Answers: 0

    I also notice that it then fails to create a "No records" table and so I get an error in the console:

    Uncaught TypeError: Cannot read property 'length' of undefined

    I think it should fail gracefully.

  • allanallan Posts: 61,765Questions: 1Answers: 10,111 Site admin

    I think it is unwise to presume that this is the wording I would want to show my users.

    I would suggest never showing these messages to your users :-). They are errors that are there for the use of developers only. If an error is shown then there is a problem that should be fixed.

    I also notice that it then fails to create a "No records" table and so I get an error in the console:

    Please link to a test case showing the issue so I can debug it.

    Allan

  • FurburgerFurburger Posts: 37Questions: 8Answers: 0

    Try this:

    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="css/dataTables/css/jquery.dataTables.min.css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript" charset="utf8" src="//datatables.net/download/build/nightly/jquery.dataTables.js?_=5d4bb17083b5dcd5b9a244c8b597c6cf"></script>
    <title>Matters</title>
    </head>
    <body>
    
      <table id="myTable" class="display dataTable no-footer" cellspacing="0" width="100%" role="grid" style="width: 100%;">
      </table>
    </div>
    
    <script>
    
    $.fn.dataTable.ext.errMode = function ( settings, helpPage, message ) { console.log(message)};
    
    var myTable = $('#myTable').DataTable({
    
            ajax: {
                url: "Test.json",
                type: 'POST'
            }
    
    });
    </script>
    
    </body>
    </html>
    

    Test.json

    {"error": "My custom error"}

  • allanallan Posts: 61,765Questions: 1Answers: 10,111 Site admin

    I see - thanks. As a workaround at the moment you could either:

    1. Add an empty data array to the JSON return.
    2. Throw an error in your error function to stop the Javascript execution.

    I'll have a think about the best way to handle this in DataTables core.

    Allan

This discussion has been closed.