Error handling with Tesla

Hi, I’m using Tesla for a fun project, and I noticed it “explodes” instead of, for example, returning an {:error, something} tuple in case something goes wrong.

iex(2)> Tesla.get("http://foobar.baz")
** (Tesla.Error) adapter error: :econnrefused
    lib/tesla/middleware/core.ex:10: Tesla.Middleware.Normalize.normalize/1

How to handle this? Should I use a try block? What if the Tesla call is inside an Enum.map over many urls? Is there an idiomatic way to handle this?

1 Like

Try is basically what you would need for this unless it exposes another API (if get throws it really should be get!, bleh…

The documentation doesn’t even mention to throw or raise, and as far as I was able to scan the docs, there is no tupled version of the functions available.

In my opinion the API should be reworked drastically! Catching is expensive, and not everyone wants to just crash…

1 Like

Yeah I guess it’s a bit odd, might be the case to switch to another more “educate” library like HTTPipe, because honestly it doesn’t seem such an incredible occurrence to try to GET a url that doesn’t exist for an HTTP library :slight_smile:

1 Like

Before switching to any other library I would recommend to raise an issue on Tesla github repo and ask the maintainer of the library for improvement or for reasoning why it was designed that way: https://github.com/teamon/tesla

1 Like

This has been brought up in the past and just now - please take a look at https://github.com/teamon/tesla/issues/64 and https://github.com/teamon/tesla/pull/81
As you can see, it is very easy to change this behaviour to whatever suits you best - tesla was designed to be extensible as much as possible. I’m happy to continue the discussion in tesla’ github issues.

I should’ve probably ignore this, but as a active member of elixir community I feel obligated to do my best to make it a nice and helpful place for everyone and stand up to undesired behaviour.

I know having issues with 3rd party libraries can be frustrating. But please remember that most of the time these libraries are made by people for free, with their own free time. Pointing that something “should” be done this way or another is not a good way to start a discussion. Not to mention the rest of pointless, ranting comments here. This is the perfect example of making a hostile community.

Thank you @vmoravec for a reasonable approach to this issue. That’s how we can get nice things.

12 Likes