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?
Most Liked
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.
Popular in Questions
Other popular topics
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
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








