Shenrak
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 aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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