datatable.ajax.reload() ... any error handling options?

datatable.ajax.reload() ... any error handling options?

TheStormsOfFuryTheStormsOfFury Posts: 10Questions: 1Answers: 0

I'm using the ajax.reload() feature to update my data every XX minutes: https://datatables.net/reference/api/ajax.reload()

I have the following code to refresh my data every 10 minutes.

setInterval(() => {
dataTable.ajax.reload(null, false);
},600000);

I'm trying to work on some error handling if the database is down or there is an error and redirect to a specific error page specifically if the database is overloaded and has an 'ETIMEDOUT' error, or similar? Currently, the return is giving me JSON error because I'm trying to res.render the new page.

Is there anything that can accomplish this?

Answers

  • allanallan Posts: 64,216Questions: 1Answers: 10,598 Site admin

    You could listen for dt-error and redirect as needed. You can also use DataTable.ext.errMode to control what DataTables does when it encounters an error.

    Allan

  • TheStormsOfFuryTheStormsOfFury Posts: 10Questions: 1Answers: 0
    edited March 27

    @allan so my datatable is named "customer_data" created through dataTable = new DataTable. Does the following look right, or where should I put the ".on" error?

    $('#customer_data')
         .on('dt-error.dt', function()  {
               window.location.replace(window.location.host);
         })
    
  • TheStormsOfFuryTheStormsOfFury Posts: 10Questions: 1Answers: 0

    What is now working:

    dataTable.ajax.reload(null, false)
    $('#customer_data')
        .on('dt-error.dt, function () {
            window.location.replace(window.location.host);
        }
        .DataTable();
    

    This does redirect to the main page which I have already checking for a db connection.

    I am going to try switch 'function()' with '() =>' to see if it still works.

    Thanks for pointing me in the right direction @allan

  • TheStormsOfFuryTheStormsOfFury Posts: 10Questions: 1Answers: 0

    yeah () => did not work.

    Random question though: does this need to be in the setInterval() block, or can it be anywhere?

    Thanks again @allan

  • kthorngrenkthorngren Posts: 21,838Questions: 26Answers: 5,048
    edited March 27

    Place the dt-error event before the Datatables initialization. The ajax.reload() will execute sometime after. If you want to refresh the table data every 10 minutes then do something like this:

    $('#customer_data')
         .on('dt-error.dt', function()  {
               window.location.replace(window.location.host);
         })
         .DataTable({
              // init options
         });
    
    setInterval(() => {
        dataTable.ajax.reload(null, false);
    },600000); 
    
    

    If you still need help please provide a simple test case showing what you are doing so we can help debug. You can use one of the JS BIN Ajax templates found here.

    Kevin

  • TheStormsOfFuryTheStormsOfFury Posts: 10Questions: 1Answers: 0

    So ... it worked once ... i made that change and it didn't anymore ... thank you nodemon but yeah, I don't think it completely refreshed the code.

    Trying it outside of the setInterval() block

  • kthorngrenkthorngren Posts: 21,838Questions: 26Answers: 5,048
    edited March 27

    I'm not sure what exactly you have done soI built a simple test case to demonstrate:
    https://live.datatables.net/socirone/48/edit

    First it uses DataTable.ext.errMode to turn off the default alert message. You can use throw or none depending on your requirements. Added the use of ajax.url() to change the URL to something that will cause an error after the first ajax.reload(). If this doesn't help then please update my test case with that you are doing so we can help debug.

    Kevin

  • TheStormsOfFuryTheStormsOfFury Posts: 10Questions: 1Answers: 0

    Actually, it did not work. I hit the "refresh" button and forgot about it.

    Still trying to make this work! lol

  • kthorngrenkthorngren Posts: 21,838Questions: 26Answers: 5,048
    edited March 27

    Actually, it did not work. I hit the "refresh" button and forgot about it.

    What exactly happens? Do you get other errors in the browser's console? More information is needed to help than to say it didn't work.

    Did you see my above test case?

    Kevin

  • TheStormsOfFuryTheStormsOfFury Posts: 10Questions: 1Answers: 0

    @kthorngren it did not show me your comments until just now.

    I had the .on block after the initialization. I just moved it to before the initialization, and I'm going to test it now. I'll also look at your js bin sample!

    Thank you!

  • TheStormsOfFuryTheStormsOfFury Posts: 10Questions: 1Answers: 0

    Okay, i see exactly how this is supposed to work now thanks to your js bin sample.

    I'm running to dinner and I will implement how you outlined.

    Quick question @kthorngren do I want 'throw' or 'none'?

    Thank you again! I'll update later this evening.

  • allanallan Posts: 64,216Questions: 1Answers: 10,598 Site admin

    throw will throw an error to the Javascript console and stop the execution of the current "thread". none will ignore the error and quite likely trigger an unhandled exception somewhere else down the chain.

    Personally I'd throw an error, but its up to you :)

    Allan

  • TheStormsOfFuryTheStormsOfFury Posts: 10Questions: 1Answers: 0

    @kthorngren I finally had time to try your solution, but sadly, it is not working. Here is my code:

    DataTable.ext.errMode = 'none';
    
    let dataTable = $('#customer_data')
                .on('dt-error.dt', function() {
                    console.log(`This is the dataTable dt-error catch.`)
                    window.location.replace(`http://` + window.location.host + `/errorPageETIMEDOUT`);
                })
                .DataTable({
                    'processing' : true,
                    'serverSide' : true,
                    'serverMethod' : 'get',
                    'ajax' : {
                        'url' : '/dashboard/get_data',
                        'data' : function ( data ) {
                            data.searchProcessed = processedCheckbox;
                            data.searchArchived = archivedCheckbox;
                        },
                        'timeout' : 15000
                    },
                    'aaSorting' : [],
                    'columns' : [
                        {
                            className: 'dt-control',
                            orderable: false,
                            data: null,
                            defaultContent: ''
                        },
                        { data : 'dateReceived' },
                        { data : 'form_type' },
                        { data : 'form_status' },
                        { data : 'reviewed_by' }
                    ],
    
                });
    
    setInterval(() => {
                dataTable.ajax.reload(null, false)
            }, 60000);
    
    

    My question, and what I'm about to try is the "timeout"causing an issue, because we are returning the results which should be erroring out; however, there is no change, and it should be redirecting to the error page.

  • TheStormsOfFuryTheStormsOfFury Posts: 10Questions: 1Answers: 0

    @kthorngren I just removed timtout and changed err to throw and my timer is 60 seconds and I've been waiting for it to throw an error for about 3 minutes in the console now, and nothing.

  • allanallan Posts: 64,216Questions: 1Answers: 10,598 Site admin

    Perhaps you can link to the page in question so we can take a look?

    Allan

  • kthorngrenkthorngren Posts: 21,838Questions: 26Answers: 5,048
    edited 11:14AM

    what I'm about to try is the "timeout"causing an issue

    Are you saying you are creating this timeout at the server?

    because we are returning the results which should be erroring out

    What errors are being returned? Are they jQuery Ajax() errors or some other type of error?

    What is the status code when you are expecting an error?

    Have you tried to simply use an invalid URL, similar to my example, to test your code?

    Use the browser's network inspector to monitor the XHR requests and response to make sure they are what you expect when generating the errors. As Allan says for us to help debug we will need a link to a test case showing the issues.

    The ajax option uses jQuery Ajax(). You could try testing with the jQuery Ajax error handler to see if it fires.

    Kevin

Sign In or Register to comment.