fireproofsocks

fireproofsocks

Examples of best practices for handling bad requests in Phoenix?

I’m wondering if anyone has any educational (i.e. simplified) examples that demonstrate best practices when it comes to handling bad requests in a Phoenix application. For educational purposes, let’s consider just one case:

Creating a record: how to handle a bad payload
Maybe your router has something like this:

  scope "/api", MyAppWeb do
    resources("/things", ThingController)
  end

And maybe your controller has something like this:

  def create(conn, %{"data" => data}) do
    with {:ok, thing} <- ThingCreate.create(data) do
      conn
      |> put_status(:created)
      |> render("thing.json", thing: thing)
    end
  end

You’ll notice that the route won’t pattern-match if your payload is something like [] (an empty JSON array)…

It’s tempting to write another controller function that would handle the bad data:

  def create(conn, _bad_data) do
    # return an error...

but that’s just smelly

This article looked promising, but it didn’t get into details: https://medium.com/appunite-edu-collection/handling-failures-in-elixir-and-phoenix-12b70c51314b

I think that the correct response for this situation would be a 400 (e.g. 422), but not simply a 404. What is the cleanest way to return an error message with specifics and a proper status code? Is this best handled in the fallback controller? Does anyone have a complete example of this?

Thanks for sharing!

First Post!

dimitarvp

dimitarvp

I don’t see that, you don’t have any additional assertions in the function’s head save for the map requirement with a singular "data" key – nor do you have when clauses. Why wouldn’t this:

def create(conn, %{"data" => data}) do

…match %{"data" => []}? Or you meant if the object itself is not a map at all and just []?

Assuming it’s the latter, then:

…is not a factual statement. In our team we use pattern-matching function heads all the time and they really do help readability and expressing your intent much better than huge case or cond arms in the code of a single controller action function.

Last Post!

dimitarvp

dimitarvp

Sure, you caught me late at night not thinking entirely straight. Be proud! :stuck_out_tongue:

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement