pmjoe

pmjoe

How to improve Ecto error handling?

I do like a lot the concept of errors as values, and the pattern match that can be done in Elixir. I’m building an Elixir app right now and I’ve reach the point where I need to interact with the database. I did configured Ecto, and everything is working alright, but I have a big question in my head regards to error handling with Ecto.

Reading Ecto documentation there is very little about error handling, and what I usually see is exceptions being throw most of the times, even on methods like Repo.one, without the exclamation, where it returns the element, nil, or in case of something else, throw an exception.

Maybe I’m trying too hard to catch all the errors, and there are some errors that are really exceptions that should be raised to an upper level? I don’t know, it just feels wrong. I would prefer to handle it, even if it’s just a log entry, and then return from the function with a {:error, ...}. For a moment I thought that the Repo.one was just an exception, but then Repo.all behaves the same.

Any tips? I’m trying to hard to catch these errors? Or Ecto is really too heavy on the exception side? What is your opinion?

First 2 of 2 Posts Switch mode

arcanemachine

arcanemachine

Ecto documentation is pretty old in a lot of places, and I wouldn’t necessarily take it as a best practices guide for my application programming in general. It just covers so many use cases that sometimes some contrived examples would be needed.

I would probably stick to error handling as needed. Use with blocks to handle your happy path cases, then use the fallback controller (if you’re using Phoenix) to handle common errors, e.g. changeset errors. (If you’re not familiar, this is what the Phoenix generators spit out, I believe it’s the phx.gen.html generators that would have examples). For one-off “errors”, just use a custom with...else statement to handle that specific error. Then add common errors to the fallback controller as needed.

The bang functions can be useful in certain circumstances. For example, if using Repo.one! in a controller, that can automatically return 404 in a Phoenix controller. (I believe this is the default Phoenix behavior, my Phoenix is a little rusty ATM.)

LostKobrakai

LostKobrakai

My perspective is the Repos job is executing queries and dealing with resulting data. Any failures of the way queries or results are shaped like only produce exceptions on bang functions.

Repo however expects a working connection to the db. So any problems with that are indeed exceptional and therefore raise exceptions. Problems with connections should be dealt with at the level of connection handling / the db pool. Not at the level of some code trying to run a query.

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
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
spammy
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
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
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
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
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
ausimian
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
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement