lud

lud

Why is Phoenix default ErrorJSON using plural errors with a map as the value?

Hello,

By default Phoenix will generate this on a new project:

# def render("500.json", _assigns) do
#   %{errors: %{detail: "Internal Server Error"}}
# end

def render(template, _assigns) do
  %{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
end

Not sure if it is because of an english thing (not my native language) but to me it seems that a map would describe a single error ; plus we are giving the detail for one error.

When I’ll have multiple errors, on an Ecto changeset for instance, I’ll generally return something like that:

%{
  error: %{
    message: "Invalid Request",
    detail: %{
      errors: [] # ... changeset errors as a list
    }
  }
}

Or maybe something like this:

%{
  errors: [
    %{
      message: "Invalid Request",
      detail: [] # ... changeset errors as a list
    }
  ]
}

But I don’t understand the default layout, which is one of the first things I change on a new project.

What is the rationale behind this?

Thank you.

Most Liked

lud

lud

Hello,

Well I’m asking what is the rationale beyond those choices. Of course I can change it.

Code gen is only there to get someone started. It’ll never be 100% correct for all use cases

My problem here is that is has been correct for 0% use cases in my experience. We always changed it. I wonder why the current layout was chosen.

jdiago

jdiago

I dug a little deeper and this is my speculation:

This bit is in the MyApp.ErrorJSON module.

When you use mix phx.gen.json, you will get a resource_json.ex which will have the following:

  def error(%{changeset: changeset}) do
    errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)

    %{errors: errors}
  end

Seems to me like lud’s suggested change is supposed to go into a controller’s JSON module.

Notice the matching :errors root key. That probably makes it easier for API clients to handle errors no matter where it’s coming from (an action on a resource or somewhere up the plug chain)

LostKobrakai

LostKobrakai

Ecto.Changeset.traverse_errors/2 returns a list though. Not a single object.

Where Next?

Popular in Discussions Top

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
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&*())^% %%:>" From this string, I want to only keep the integers, ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
mmport80
I have put far too much effort into Dialyzer over the last year or so - and basically - I doubt it’s worth the effort. It’s not as easy ...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Markusxmr
Since Drab has been developed for a while in the open, introducing the Liveview functionality in a way it happend appears to undermine th...
New

Other popular topics Top

chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43591 214
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement