shahryarjb

shahryarjb

How to stop 500 errors when UUID doesn't match

I have an 500 error when my user changes UUID which is relation to Category UUID for inserting post.

** (exit) an exception was raised:
    ** (Ecto.ChangeError) value `"a863228-727c-4cf3-93f5-9b2f79df1288"` for `TrangellCmsService.Cms.Db.Post.cms_post_category_id` in `insert` does not match type :binary_id

How do I prevent an 500 error in client request? I want to show 400 error instead of 500 error , how do ?

my changeset :

def changeset(struct, params \\ %{}) do
        struct
        |> cast(params, @all_fields)
        |> validate_required(@all_fields)
        |> unique_constraint(:seo_alias_link, name: :index_of_post_alias_unique_link, message: "alias link already exists.")
        |> validate_inclusion(:seo_language, ["en", "fa"])
        |> validate_inclusion(:group_acl, ["admin", "actived", "unactived", "blocked"])
        |> validate_inclusion(:post_type, ["article", "shop", "free"])
        |> validate_length(:title, min: 3, max: 100)
        |> validate_length(:description, min: 100)
        |> validate_length(:seo_words, min: 3, max: 100)
        |> validate_length(:seo_description, min: 50, max: 264)
        |> foreign_key_constraint(:cms_post_category_id)
    end

Most Liked

rwngallego

rwngallego

In Ecto3 when you specify in your model the primary key you can use Ecto.UUID instead of :binary_id so that the Ecto casting works correctly:

@primary_key {:id, Ecto.UUID, autogenerate: true}
schema "post" do
  ...
end

This way you should get the regular Ecto validations before going to the DB which looks like is what you want: {"id":"is invalid"}

From the ecto documentation:

… (typically :id or :binary_id , but can be any type)

kokolegorille

kokolegorille

Maybe You can test like this

iex> Ecto.UUID.cast "a863228-727c-4cf3-93f5-9b2f79df1288"
:error
iex> Ecto.UUID.cast Ecto.UUID.bingenerate()              
{:ok, "767f998a-424b-4270-beff-742b93f636fc"}
Ankhers

Ankhers

It also depends on the properties you want your program to have. In this specific case I would prefer to return an error to the client saying that the UUID specified is invalid.

Last Post!

rameramwe

rameramwe

Thanks!

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement