gmc

gmc

I’ve created a JSON API and deconstruct parameters in the controller action like this:

def create(conn, %{“item” => item_params}) do

end

Now I’d like to capture the cases where the pattern isn’t matched (e.g. not having the “item” in the request JSON) and send an appropriate error JSON.

How can this be done? Now I just get “no function clause matching in …create/2” error. After some research I found the following issue which would suit my needs, but it was closed by José:
https://github.com/phoenixframework/phoenix/issues/2386

Showing Posts 1 to 10

Nicd

Nicd

Write a second clause for the function:

def create(conn, params)

def create(conn, %{“item” => item_params}) do
  do_stuff()
end

def create(conn, _params) do
  send_error()
end
gmc

gmc OP

I actually thought about this, but then I asked me, how can Elixir know which function to take for a valid request? In that case both functions would match, right? How is the priority handled in Elixir? Sorry, this is a very basic question as I’m new to Elixir/Phoenix…

stefanchrobot

stefanchrobot

The pattern matching is applied top to bottom:

# This is optional.
def create(conn, params)

# Attempted first, matches on correct params.
def create(conn, %{“item” => item_params}) do
  do_stuff()
end

# Matches everything else.
def create(conn, _params) do
  send_error()
end
gmc

gmc OP

Ah, thanks for this, that’s essential to know!

Sebb

Sebb

I just wanted to point to the documentation about pattern matching in functions, but there seems to be none. Neither in elixir-hexdocs nor in the guide…? Am I missing something?

stefanchrobot

stefanchrobot

How about https://elixir-lang.org/getting-started/pattern-matching.html

EDIT: oh, I see. There’s no mention of pattern matching in function heads.

Sebb

Sebb

There is sth here Functions · Elixir School
But its not part of the official docs.

adamu

adamu

I am also confused by this.

José Valim here:

The ActionClauseError is already rendered as the appropriate 400, so you likely don’t need to do anything here.

Chris McCord here:

This is as designed since we don’t wrap and translate errors when debug_errors is enabled. It is false by default, so test and prod will return. your 400 as you want. Thanks!

The phoenix docs:

to provide custom error pages, we could simply define a proper render/2 function clause in HelloWeb.ErrorView.

However, in all the examples provided above and in the linked GitHub issues, and in my test, the error is not handled by Phoenix, the Plug.Exception protocol is unused, and the program crashes with the uncaught exception (a generic 500 error is thrown if accessing via the browser, exception is uncaught in tests).

Is something supposed to catch these Plug.Exception exceptions and route them to the ErrorView, or is this only for debugging?

LostKobrakai

LostKobrakai

It works fine for me here:

https://github.com/LostKobrakai/repo_exception/commit/550e60c29d5764dbfd3e07a9cf0d2465a4f7c4fe

One thing to note is that the Phoenix.ConnTest API is not meant to catch exceptions as they skip the outer http parts of handling a request. You also generally want the exceptions details in tests to be able to debug issues. Other frameworks usually need code to disable that.

You can test Plug.Exception working though by enabling the server in the test env and doing full http requests.

adamu

adamu

Thank you! ConnTest behaving differently to HTTP requests was tripping me up, thanks for pointing that out - and for the demo. Super helpful.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews