Qqwy

Qqwy

TypeCheck Core Team

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:

  1. tuple as an object
{:ok, 1} -> {"tag": "ok", "payload": 1}
{:error, 2} -> {"tag": "error", "payload": 2}
  1. tuple as a two-value array
{:error, 2} -> ["error", 2]
  1. 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

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

sasajuric

Author of Elixir In Action

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

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

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.

Where Next?

Popular in Discussions Top

AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 20046 166
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18584 126
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement