lud

lud

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.

Showing Posts 1 to 8

lud

lud OP

@chrismccord Maybe you can shed some light on this!

jdiago

jdiago

You sort of answered your own question.

Code gen is only there to get someone started. It’ll never be 100% correct for all use cases and it’s almost never the code that ends up in production. It’s not a crime to change the code generators give you.

As a last resort, you can also make your proposed changes to the generator and open a PR to see if it’ll get accepted.

lud

lud OP

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.

LostKobrakai

LostKobrakai

I’d propose a PR changing it to something more useful then. To me this doesn’t look like a very intentional format.

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.

jdiago

jdiago

errors: [...] makes it grammatically correct.

If an error comes from somewhere up the plug chain, MyApp.ErrorJSON is used which will return {errors: ...}. If the error is on a create or update action, the resource’s JSON module will also return {errors: ...}.

That would mean that I, as the API consummer, would only have to worry about handling 1 errors root key instead of handling both error and errors.

lud

lud OP

But you would not know if its a list or an object. Hence the second “correct” example I gave where errors is always a list.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews