hawkyre
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.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
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
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
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
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
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
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
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex











First 2 of 2 Posts
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.hawkyre
I’m not using phoenix so I need to parse the errors anyway in the frontend. Some of the things you mention I assume are automatically handled by phoenix if you return a changeset with errors somehow to the frontend