Handle and catch Ecto errors / exceptions

I need to handle and catch ecto exceptions to return my custom exceptions, my current functions is :

  def list_books(filters) when is_list(filters) do
    try do
    Repo.all(from Book, where: ^filters)
    rescue
      error -> IO.inspect(error)
    end
  end

and this the result of Inspect :

** (Protocol.UndefinedError) protocol Enumerable not implemented for %Ecto.Query.CastError{message: 
"lib/booker/booker.ex:123: value `\"created├®├®\"` in `where` cannot be cast to type {:parameterized, 
Ecto.Enum, %{mappings: [created: \"created\", sold: \"sold\", canceled: \"canceled\", failed: \"failed\"], 
on_cast: %{\"canceled\" => :canceled, \"created\" => :created, \"failed\" => :failed, \"succeeded\" => 
:succeeded}, on_dump: %{canceled: \"canceled\", created: \"created\"`

This error for example is when I passed an invalid Enum value (not existing in database).

Is there way to get error.name or code for example ? to handle it (instead of inspecting it)

Thanks

The output you posted looks like what would happen if the caller of list_books tried to use the CastError where it expected a list, something like:

Enum.each(list_books(filters), fn book -> ... end)

since IO.inspect returns its argument.

2 Likes

Why do you think you need this? I could be wrong but I think try/rescue is expensive, and it’s generally avoided in Elixir code. Why not just let the Ecto error raise itself? Or maybe better, you could validate the input and raise a custom exception before hitting the database.

Whatever you return from rescue will be the return from the try do block, and as @al2o3cr said, IO.inspect returns its argument. In your rescue block you can do anything you want with the rescued exception, but if you don’t want execution to continue to the next function call you’ll need to reraise.

1 Like