dogweather

dogweather

Ecto error handling: let exceptions bubble up?

In Ecto programming, could this be described as an anti-pattern?

# Database operation
Repo.some_operation(...)
rescue
  error ->
    Logger.error("Failed to do X: #{inspect(error)}")
    {:error, "Generic message"}  # ← System failure as business error

It seems like the answer is yes. I’m in the process of ripping out a bunch of this in a codebase.

FYI, the Dialyzer unmatched_return was the hint that I had a problem with my error handling. I’m a believer now.

EDIT: Here's ChatGPT o3-pro's answer to the question

Short answer – yes, it is an anti‑pattern.

Catching every exception that can bubble out of Repo.* and then returning a domain‑style {:error, …} tuple does more harm than good in most Elixir/Ecto codebases. Below is a breakdown of why it’s problematic and what to do instead.


1. What Ecto and Elixir’s “happy path” expect

Condition Normal, non‑bang call ( Repo.insert/2**,** Repo.update/2**, …)** Bang call ( Repo.insert!/2**, …)**
Validation / constraint failure (expected business error) {:error, %Ecto.Changeset{…}} Raises Ecto.InvalidChangesetError
DB connectivity, malformed SQL, serialization failure, etc. (system error) raises (DBConnection.ConnectionError, Postgrex.Error, …) raises (same)

Take‑away:

  • Business failures are already represented by return tuples.
  • System failures are already raised.You almost never need a rescue to tell them apart.

2. Why wrapping raised system errors in a generic

{:error, _}

is harmful

  1. Loses signal The caller can no longer discriminate between user‑facing problems (e.g., “email already taken”) and infrastructure problems (e.g., “couldn’t reach the database”). They both look like {:error, “Generic message”}.
  2. Breaks fault tolerance Elixir’s supervision philosophy is “let it crash.” A system error usually should crash the process so the supervisor can restart it (and you get a crash report with stack‑trace). Converting it into an ordinary return value prevents that safety net.
  3. Hides stack‑trace & root cause You log inspect(error), but unless you also log STACKTRACE, you’ve lost the most useful bit. Even then, the caller still has no context.
  4. Encourages defensive coding everywhere else Downstream functions must now handle phantom {:error, “Generic message”} tuples that were never in the Ecto contract, leading to boilerplate and accidental swallowing of real domain errors.

3. Better patterns

3.1 Use non‑bang APIs and pattern match

case Repo.insert(changeset) do
  {:ok, struct} ->
    {:ok, struct}

  {:error, %Ecto.Changeset{} = cs} ->
    {:error, cs}           # business error – propagate the changeset
end

If Repo.insert/2 raises (network down, bad migration, etc.) let it crash. Your supervisor, the Logger backend (or Sentry/AppSignal/etc.), and any retry strategy will handle the rest.

3.2 Use bang APIs

inside

supervised tasks

If you conceptually want “success or crash,” call the bang version in a short‑lived process you own (Task, GenServer handle‑call, etc.):

def create_user!(params) do
  changeset = User.changeset(%User{}, params)
  Repo.insert!(changeset)   # raises on infra failure OR bad changeset
end

The caller can choose whether to wrap that in try/rescue or link/monitor the task.

3.3 Translate only

expected

DB error codes

Occasionally you truly need to map a low‑level PG error to a domain error (e.g., unique‑violation for an idempotency key). Catch just that error and translate, preserving everything else:

try do
  Repo.insert!(row)
rescue
  e in Postgrex.Error ->
    if e.postgres.code == :unique_violation do
      {:error, :duplicate_key}
    else
      reraise e, __STACKTRACE__
    end
end

4. What to do with logging

  • Let your crash reporter or Logger backend capture uncaught errors globally.
  • Log business errors where they are handled (often in Phoenix controllers, Absinthe resolvers, or the calling service layer), not inside a blanket rescue.

5. When might rescuing be appropriate?

  1. External API timeout that you must degrade gracefully (still distinct from business logic).
  2. Known, isolated DB errors where retry or fallback is viable (e.g., transient serialization failures in a saga pattern).
  3. Migration scripts / mix tasks where you want the script to continue processing subsequent items.

Even there, rescue only the specific exception types you care about and keep the original Exception.message/1 so the operator can see what went wrong.


Bottom line

Rescuing all Repo. exceptions and returning {:error, _} conflates system failures with domain logic, obscures diagnostics, and undercuts OTP supervision.*

Prefer the natural Ecto contracts—pattern‑match on {:error, changeset} for business cases and let unexpected failures crash (or handle them narrowly).

Adopting this discipline keeps your codebase idiomatic, debuggable, and resilient.

Most Liked

camatcode

camatcode

Let it crash.

The only time I can think of when I catch ecto errors like that is when I want an invalid UUID to be the same as a UUID that doesnt exist

  @impl true
  def one(query, opts \\ []) do
    super(query, opts)
  rescue
    err in Ecto.Query.CastError ->
      if err.type == Ecto.UUID or match?({:in, Ecto.UUID}, err.type) do
        Logger.warning("Received invalid UUID #{err.value} in query #{inspect(query)}")
        nil
      else
        reraise err, __STACKTRACE__
      end
  end
tfwright

tfwright

The question you’re directly asking here has been thoroughly answered, and in fact it’s explicitly covered in Elixir docs, so you might want to give that whole section a read: Design-related anti-patterns — Elixir v1.20.2 (although personally I find that some of those issues are more stylistic decisions that come with tradeoffs than “genuine” anti-patterns).

But to go a bit further, since it sounds like you are new to the library… if you are getting exceptions from Ecto something may have already gone wrong. As you can see from the docs, most Ecto operations do not raise errors by default, instead they return error tuples already so there is no exception to let bubble up or not, and you have to use a separate ! version of the function to get it to raise, so you may want to check you are using the right API for your case. The operations that do raise errors “unexpectedly” mostly do so because the inputs are invalid (for example if you pass in value with a data type that doesn’t match the db field). In those cases you are missing some sort of validation higher up the call chain. If you want to provide more context about where you are running into this issue, we might be able to offer more specific advice.

krisleech

krisleech

I never knew about super

Where Next?

Popular in Questions Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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

Other popular topics Top

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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement