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

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
You could listen for
dt-error
and redirect as needed. You can also useDataTable.ext.errMode
to control what DataTables does when it encounters an error.Allan
@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?
What is now working:
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
yeah () => did not work.
Random question though: does this need to be in the setInterval() block, or can it be anywhere?
Thanks again @allan
Place the
dt-error
event before the Datatables initialization. Theajax.reload()
will execute sometime after. If you want to refresh the table data every 10 minutes then do something like this: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
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
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 usethrow
ornone
depending on your requirements. Added the use ofajax.url()
to change the URL to something that will cause an error after the firstajax.reload()
. If this doesn't help then please update my test case with that you are doing so we can help debug.Kevin
Actually, it did not work. I hit the "refresh" button and forgot about it.
Still trying to make this work! lol
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
@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!
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.
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
@kthorngren I finally had time to try your solution, but sadly, it is not working. Here is my code:
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.
@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.
Perhaps you can link to the page in question so we can take a look?
Allan
Are you saying you are creating this timeout at the server?
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