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.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement