Hacker News new | ask | show | jobs
by ricketycricket 1036 days ago
> no error checking at all (I assume it just panics or exception?)

In Elixir, bang functions per convention will raise on error. `get/2` will return error tuples allowing you to handle errors. In fact, get!/2 just calls get/2 and raises for you[^1].

> no mention of JSON at all

Req is the most "batteries included" Elixir HTTP lib out there. I can't speak for Wojtek, but I believe the goal was to make Req extremely easy to use in scripting or things like LiveBook without having to do much work. That being said, the automatic decoding is mentioned in the readme[^2] and the docs[^3].

> if "body" is JSON, how do you even get the raw body, or can you?

Per the docs[^3], you can either skip with the `:decode_body` option, or just build your own request using only the steps you want.

> just seems over engineered/over fitted whatever you want to call it.

Fair, but again, this library is designed to be on that end of the spectrum. There are plenty of other libraries further down the stack that you can use. I am partial to Finch[^4], upon which Req is built.

To address the sibling comment about "Let it Crash", the language allows you to easily recover from crashes, but that is for resiliency, not error handling. In practice you would use the non-bang get/2, pattern match on the response, handle any errors, perhaps use Kernel.get_in/2 to safely traverse the map, etc. The example provided by the author is not "production ready".

[^1]: https://github.com/wojtekmach/req/blob/v0.3.11/lib/req.ex#L3...

[^2]: https://github.com/wojtekmach/req

[^3]: https://hexdocs.pm/req/Req.Steps.html#decode_body/1

[^4]: https://github.com/sneako/finch

1 comments

Thanks for detailing while I was asleep :-)

Indeed, that + pattern matching does error checking just right.

I must admit that the automatic JSON decoding initially turned me off a bit!