Retrying failed requests


#1

Hello,

I’m curious if there is a way to retry a request that responds with an { action: “error” }? In my application, all requests send an access token to the backend, to identify the user, and if the token is expired, the backend returns an error, telling the client to refresh their token. However I need to be able to retry the original request after refreshing the token.

Thank you for any input.


#2

Hello @krv,

If you are using the dataProcessor to connect with the backend, you can define logic you need on the onAfterUpdate event:
https://docs.dhtmlx.com/api__dataprocessor_onafterupdate_event.html

In the following way:

dp.attachEvent("onAfterUpdate", function (id, action, tid, response) {
	if(response.action == "error"){
		doSmthOnError()
	}
})

#3

Thanks. I did read about “onAfterUpdate”, but I guess my question is, how do I re-execute the same exact API call to the backend, with the exact same request data, without having to manually resend this request?


#4

Hello @krv ,

You can resend the failed request with the dp.sendData(id) method:
https://docs.dhtmlx.com/api__dataprocessor_senddata.html
on the error action:

dp.attachEvent("onAfterUpdate", function (id, action, tid, response) {
	if (response.action == "error") {
			dp.sendData(id)
	}
})

So the dataProcessor will resend data till the server will respond without error:

If it’s still not the thing that you need, could you please clarify your requirement?
By default dp doesn’t resend data after the error response, and if you want to do this, you will have to define some action on the error response.


#5

Thanks, this is exactly what I was looking to do for updates.

One more question though, how can I retry the ‘scheduler.load’ request if that also fails?

I tried the following…

scheduler.attachEvent("onLoadError", function(resp){
    // Do other stuff here...
    // End do other stuff...
    scheduler.load(URL); // This doesn't make another request?
}

But scheduler.load(URL) doesn’t make another request…


#6

Nevermind i figured it out.

scheduler.attachEvent("onLoadError", function(resp){
    // Do other stuff here...
    // End do other stuff...
    scheduler.clearAll();
    scheduler.load(URL); // This doesn't make another request?
}