How to deal with server-side errors when working with API?

shYtsg89 report abuse

I am developing a Python application which is reliant on two 3rd-party APIs. Very rarely one of the APIs returns 503 errors. I suppose this is due to server maintenance or something like this. It is a rare case, but it breaks my application. What can I do?


MaleFisent report abuse

It is not your fall that API provider cannot support smooth API operation. My advice is to just change the architecture of the app to become not so reliant on the API. Probably it will not be a big problem if some functionality will be inaccessible rarely. But if failed API can completely break your application then it is not a good app.

Radek report abuse

You can wrap the API call in try/except block and place it inside a loop. When you get 503 response code from API, then wait for a while and retry. And do this several times. Probably the server downtime is a rare case and this can help you not to crash an app.

jeluw report abuse

But be sure that your app can wait. The approach proposed by @Radek is good only if your app and your users can wait all this time.

Radek report abuse

Actually you should make API calls asynchronously. While your app's back-end will make retry API calls, show users something else, engage them somehow.

shYtsg89 report abuse

Cool, @Radek! Async API calls and retries should definitely help here. Thank you very much for your help!

Add Answer

Need support?

Just drop us an email to ... Show more