How to handle APi errors?

So im working with an api and basically build an wrapper around it, so everyone can use it … and its going good, but I only cover the good way - when everything is working fine etc.
but one thing I noticed it, that I somehow have to catch errors and then return them to the user such as

400 → BadRequestException
403 → AuthException
404 → NotFoundException
429 → RateLimitException
503 → MaintenanceException
(just as an example)

here is an example http function


  def get_raw_leagues(tag) do
    {:ok, data} = Client.call_api("...")
    data
  end

While Client.call_api is just HTTPoisons Base function, just wrapped into its own module.
So my actual question here is, how can I handle f.e. an error like “api is down”, “invalid tag” etc. …

It depends on what you makeClient.call_api return.

You could “catch” these errors on that layer and make it return something like {:error, ...} (for example {:error, :bad_request}), when those errors happen.

Then you could do pattern matching on the tuple and in case of error you could propagate it or handle it there. To avoid doing a big pattern matching with {:ok, data} and all the various {:error, ...} in every http function, I would propagate it using with {:ok, data} <- Client.call_api("..."), do: data and then handle it at the “edge” before you send the reply.

Keep in mind it’s an idea I wrote just looking at your 4 lines of code so an optimal solution for your codebase might vary.