MatUrbanski

MatUrbanski

Rescue Phoenix.MissingParamError and return json

In my controller action i have two scrub_params clauses:

  plug :scrub_params, "account"
  plug :scrub_params, "studio"

When one of params is not present Phoenix raises Phoenix.MissingParamError. Is there any way to rescue this error and return some json message for user?

Marked As Solved

chrismccord

chrismccord

Creator of Phoenix
plug MyApp.ScrubParams, "account"

defmodule MyApp.ScrubParams do
  def init(key), do: key
  def call(conn, key) do
    Phoenix.Controller.scrub_params(conn, key)
  rescue
    Phoenix.MissingParamError ->
      conn
      |> Plug.Conn.put_status(:bad_request)
      |> Phoenix.Controller.render(MyApp.ErrorView, "bad_params.json", key: key)
      |> Plug.Conn.halt()
  end
end

Also Liked

chrismccord

chrismccord

Creator of Phoenix

Function bodies support rescue, catch and after as SpecialForms.try/1 does

:slight_smile:

voughtdq

voughtdq

whaaaaaaaaaaaaaaaaaaaaaat

I can’t believe I’ve missed this!

idi527

idi527

Shouldn’t there be a try somewhere? I’ve never seen rescue for def.

Heh, here it is Kernel — Elixir v1.20.2.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

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 31586 112
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement