hawkyre

hawkyre

Changeset error parsing

I want to create some kind of procedure to programmatically transform a changeset’s errors into my own error codes so that I can display them in the frontend.

I’ve come up with the following:

def create(%{} = course) do
  case Course.create(course) |> Repo.insert() do
    {:ok, course} ->
      {:ok, transform(course)}

    {:error, changeset} ->
      errors = parse_errors(changeset)
      {:error, errors}
  end
end

def parse_errors(changeset) do
  errors = changeset.errors
  parsed_errors = []

  parsed_errors = parse_single_error(errors[:tag], :unique, "COURSE_TAG_TAKEN", parsed_errors)

  parsed_errors
end

def parse_single_error(nil, _constraint, _error_msg, error_list) do
  error_list
end

def parse_single_error(error, constraint, error_text, error_list) do
  {_error_msg, constraints} = error

  if constraints[:constraint] == constraint do
    error_list ++ [error_text]
  else
    error_list
  end
end

I’m mostly looking for feedback on this since it’s the first time I’ve tried to parse errors like this and I have no idea whether there are better ways to achieve this or not.

First Post!

soup

soup

Without proofing the actual execution, it’s not unreasonable to do this.

You may also want to look at add_error as well as traverse_errors.

Most of my code has separate schemas for the data layer (business logic focused) and UI layer (user interface focused, may combine or obscure mulitiple data layer schemas - ex: data layer price is always cents but the ui layer changeset may accept dollars + cents), so with that in mind I would probably attach the error-code stuff to that as just another validation step. I may Enum.reduce(changeset.errors, changeset, ...) and just insert the error code into the existing errors.

I also would tend to return {:error, changeset} only because of convention, vs {:error, errors} but this is really code base specific – just be consistent.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
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
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
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
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

We're in Beta

About us Mission Statement