Best practice for doing API calls and handling "incorrect" responses?

So reading Best Practises for Error handling elixir? :

Say let’s say I’m doing an API call to twitter, google, or some other 3rd party service. Possible failure points could be:

1) Network time out (perhaps the network goes down somehow)
2) Bad api key (so you get back an error status message from the API)
3) etc?

  1. Would the best practice thing to do is have a supervisor “supervise” a process that makes an API call and then just let it die?
5 Likes

Hey @treble37, that can be a tough question depending on your actual requirements. From my experience working with web APIs, yes timeouts and bad requests are usually the two most frequent error scenarios. IMO, you should not really have a supervisor for that. Supervisors are meant for process related crashes from unexpected errors which you didn’t think about (or which you thought about but shouldn’t really happen if the system was developed correctly).

Since you are thinking about these scenarios (timeouts, incorrect API keys, etc…) I think the proper way is to actually program defensively against these. One may also argue that supervisors’ jobs is to restart dead or faulty processes and restarting a process which sent a bad API key will result in another error, since it’s unlikely the API Key changed in the meanwhile. This ultimately leads to the Supervisor running out of restarts and killing the subsystem.

Again, this is my opinion and your actual views about the system are the best way to judge what to do.

7 Likes

Time outs and bad requests are expected scenarios so you could pattern match on those returns from the API call, and then handle them in your server logic as you wish.

5 Likes

Thank you @sashaafm … I was looking for “best practices” for Elixir although the language may be too new for this and I wasn’t entirely sure in this particular instance…

I did come across your point about “restart dead…processes” in a slightly different context - I think the writer said that in general “error handling” the way you would do in other languages (try/catch) is to be avoided and in Elixir you let the lightweight processes die…

3 Likes

Hi, older post but wanted to bump it in case there were any new opinions on handling APIs as similar to OP, I’d like to utilize Twitter API and Google maps API. I’m new to programming and none of the online Elixir courses I’ve taken specifically cover APIs and the Swift courses I’m taking make use of the if statement (which isn’t that supposed to be a no-non generally speaking in functional programming?) so any opinions are greatly appreciated.

Additionally, with WebAssembly becoming more popular, will that impact how APIs are handled in Elixir (or in general?)

Thanks in advance! Sy

Long story short, there is nothing wrong with using an if-statement in Elixir (or another functional programming language for that matter). What is nearly always an anti-pattern, however, is an if-ladder, where multiple if-statements are nested in one-another, since usually you can rewrite this ladder as a pattern-match.

Since WebAssembly only runs in the browser front-end, it is unlikely to impact the way APIs are handled in Elixir or in general.

I hope that answers your questions :slight_smile:

3 Likes

@Qqwy - Yes it did. Thank you. Ever since finding out about the Anti-If Campaign https://francescocirillo.com/pages/anti-if-campaign , I’ve been trying to learn alternative ways of writing the code so will keep your advice in mind - one if-statement isn’t bad, but an if-ladder, try to use pattern matching instead.

Appreciate the help!

1 Like