Qqwy
Sending ok/error-tuples to the frontend - what do you use and why?
Recently on the Elm forums, a discussion about Union Types and how they are represented in other languages was started.
After telling them about the {:ok, value} | {:error, error_message} ‘type’ that sees frequent use in Elixir to represent error cases (AKA the Result or Either monad), someone had an interesting question: How do we represent these tuples when sending stuff to the frontend? (direct link to this post
Tuples themselves do not map to JSON; Poison.encode({:ok, "foo"}) will fail.
Some possibilities could be:
- tuple as an object
{:ok, 1} -> {"tag": "ok", "payload": 1}
{:error, 2} -> {"tag": "error", "payload": 2}
- tuple as a two-value array
{:error, 2} -> ["error", 2]
- discarding error messages, converting to a nullable type
{:ok, 1} -> 1
{:error, 2} -> null
Have you used any of these or another one, and why?
Most Liked
NobbZ
Personally I tend to do it like 1, also in my company we do that (though not elixir).
Our naming is a bit different though.
We tend to do it more like {"status": "ok", "data": …}/{"status": "error", "reason": …}. "reason" can, depending on how well defined the API is, be a plain string for human consumption or an object containing a numeric error code, a severity and free form “args”. But then always a human readable string is provided as well.
{
"status": "error",
"reason": {
"code": 404,
"description": "Resource 'foo' not found",
"args": { "resource": "foo" }
}
}
sasajuric
I think that a good REST API should indicate success/failure via the response status code. So returning 20x when a request has in fact failed (even if it was due to a business error) would be IMO misleading. If HTTP status codes are used, then success/failure can be distinguished on the client side by looking into the response status.
I do agree that HTTP error codes are probably too coarse grained for business errors, so providing additional context as a payload is fine.
This depends on the transports which have to be supported. For example, at Aircloak besides HTTP, we also support PostgreSQL protocol (we pretend to be a PostgreSQL database). Error reporting in that protocol has to be done in a particular way, so it’s still transport specific. Having the same shape of response sent via HTTP and via PostgreSQL protocol wouldn’t work for us.
NobbZ
Because errors in your application do not map to HTTP status codes very well.
My example with the resource not found was just an example. But if the user provided some value out of bounds, what HTTP status code would you use?
Also, if you say 404 is resource not found, how do you distinguish from the HTTP resource not beeing found vs. some resource not found which you specified in your arguments to the request?
Last Post!
baldmountain
We went down a different path. We’re deprecating all our REST APIs and replacing them with GraphQL. The only status codes you get are 500, 404 (whoops) and 200. Any errors in the server should be returned as part of the GraphQL response. Having GrapiQL available to experiment and lookup things is really useful and apollo-client works well on the front end.
Using GraphQL has been worth the effort for us.
Popular in Discussions
Other popular topics
Chat & Discussions>Discussions
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









