Shenrak
Problem with phoenix and ErrorHelpers
Hi everyone,
I am trying to use Ecto.insert, and i have the following error :
[error] #PID<0.416.0> running GORprojectWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: POST /api/rooms
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in GORprojectWeb.ErrorHelpers.translate_error/1
(gor_project) lib/gor_project_web/views/error_helpers.ex:9: GORprojectWeb.ErrorHelpers.translate_error("is invalid")
(ecto) lib/ecto/changeset.ex:2345: anonymous fn/3 in Ecto.Changeset.merge_error_keys/3
(elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/changeset.ex:2339: Ecto.Changeset.traverse_errors/2
(gor_project) lib/gor_project_web/views/changeset_view.ex:17: GORprojectWeb.ChangesetView.render/2
(phoenix) lib/phoenix/view.ex:332: Phoenix.View.render_to_iodata/3
(phoenix) lib/phoenix/controller.ex:740: Phoenix.Controller.do_render/4
(gor_project) lib/gor_project_web/controllers/room_controller.ex:1: GORprojectWeb.RoomController.action/2
(gor_project) lib/gor_project_web/controllers/room_controller.ex:1: GORprojectWeb.RoomController.phoenix_controller_pipeline/2
(gor_project) lib/gor_project_web/endpoint.ex:1: GORprojectWeb.Endpoint.instrument/4
(phoenix) lib/phoenix/router.ex:278: Phoenix.Router.__call__/1
(gor_project) lib/gor_project_web/endpoint.ex:1: GORprojectWeb.Endpoint.plug_builder_call/2
(gor_project) lib/gor_project_web/endpoint.ex:1: GORprojectWeb.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:16: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) /Users/giom/Documents/develop/web/GORProject-API/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
After some investigations, it seems that the error comes from an association not loaded, but the errors kind of triggers me because i have in my errors.pot the strings to handle this error :
## From Ecto.Changeset.put_change/3
msgid "is invalid"
msgstr ""
So i should be handled.
Here are my schemas, CRUD and controller :
Schema :
defmodule GORproject.Rooms.Room do
use Ecto.Schema
import Ecto.Changeset
alias GORproject.Rooms.Room
alias GORproject.Auth.User
schema "rooms" do
field(:depth, :integer)
field(:name, :string)
field(:description, :string)
field(:public, :boolean)
many_to_many(:users, User, join_through: "users_rooms")
has_many(:children, Room)
belongs_to(:father, Room, foreign_key: :father_id)
timestamps()
end
@doc false
def changeset(room, attrs) do
room
|> cast(attrs, [:name, :depth, :description, :public, :father_id])
|> foreign_key_constraint(:father_id)
|> validate_required([:name, :depth, :public])
end
end
CRUD :
def create_room(attrs \\ %{}) do
%Room{children: [], father: {}, users: []}
|> Room.changeset(attrs)
|> Repo.insert()
end
Controller :
def create(conn, %{"room" => room_params}) do
with {:ok, %Room{} = room} <- Rooms.create_room(room_params) do
conn
|> put_status(:created)
|> put_resp_header("location", room_path(conn, :create, room))
|> render("show.json", room: room)
end
end
Thank you ![]()
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 10 of 18 Posts
script
How you handling in create method if changeset is not valid?
Shenrak
error is sent to my fallbackController :
script
Fall back controller is okay but its better if you use the error_view file for displaying changeset errors.
you can pass your invalid changeset to this and it will display an error from that changeset .
Shenrak
I added your code in my error_view.ex, and it seems like the error is not handled there. From the stacktrace it seems that it goes from room_controller directly to changeset_view, in which there is :
script
From stacktrace it seems like in
error helpersthe translate_error function is not working for youris invalidmessage. You are passing one argument but it accepts 2 arguments.Shenrak
I totally agree with you, and this is where i do not understand. In my
errors.pot, i have the following code :According to this thread, it should be working
script
what is your goal? What you trying to implement?
Shenrak
I am trying to understand why this error is raised, how to handle it properly, and of course i am trying to fix the Repo.insert error
note that the error is not raised anymore if i delete
father: {}insideBut i then have the following error :
script
Add
IO.inspectafter|> Room.changeset(attrs)And see what it gives you in the console. After you run this method
Shenrak
i’ve put two IO.inspect :
and i have :
followed by the error stacktrace