ijunaidfarooq
I have multiple controller methods, where I am pattern matching with the coming parameters one of them is
def index(conn, %{
"id" => camera_exid,
"from" => from,
"to" => to,
"limit" => "3600",
"page" => _page
}) do
when user requests such as http://localhost:4000/v2/cameras/pocri-yweod/recordings/snapshots?limit=3600&page=1
it throws an error, and of course, it supposes to through error, but is there any way to handle such error more gracefully than an exception? without creating another index with fewer values to pattern match?
I tried creating a fallback controller very basic
defmodule EvercamMediaWeb.FallbackController do
use EvercamMediaWeb, :controller
def call(conn, what) do
IO.inspect(what)
render_error(conn, 400, "error.")
end
end
but it didn’t work.
Is it possible to make it for the whole controller? when the pattern matched parameters doesn’t match, it returns 400 while saying which params are missing? I am the only pattern matching those parameters in the head which are definite.
Trending in Questions
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
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
vfsoraki
It won’t work. Optional parameters should not be matched in the head. You should handle them in the body. Name the second parameter and use ‘Map.get’ in the body, with a default value.
A general rule of thumb is that if something does not match the head of a function, it should fail fast. It is not something you expect, so don’t handle it.
vfsoraki
As a side note, the fallback controller handles the result of your controller.
dimitarvp
Why not just make a catch-all
indexfunction without pattern-matching and log its parameters? It seems some of your externally arriving data are unexpected.al2o3cr
That’s exactly what I’d recommend you do - a head like:
expresses “all four parameters must be supplied or else error” clearly.
HOWEVER
Based on the names of the parameters in your example, I wonder if supplying defaults would be better:
ijunaidfarooq
if I have 20 def’s then having 20 _bad_params? is not an ideal solution.
is there any thing we can do? like a plug or something which can be global solution?
NobbZ
How should that single plug know which parameters would have been required?
It’s easier for each individual action with a catch all clause or just letting it crash with a generic error as it does already.
ijunaidfarooq
fallback doesn’t work. can you tell me how to make it work?
NobbZ
I haven’t said “fallback”, you did, and you have been told, it does not what you want, it can only be used to “fix” return values that are not proper
conns.Therefore I am not sure what you are asking for…
axelson
I would switch all your
defto only pattern match on the absolute required values, which in this case is probably justdef index(conn, params). But then inside the body of each function I would call a helper function that will describe the required parameters and pattern match on them, if that match fails, then the helper function can return a 400 error that describes the missing parameters.ijunaidfarooq
I got this solution from StackOverflow. do you second this?