Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phoenix Ecto how to handle NoResultsError

In my Phoenix JSON API I am getting an Ecto NoResultsError when I request an object that doesn't exist in the database.

I want my JSON API to return a null along with a 404 error.

How would I do this?

Currently I pretty much have a default generated html controller/views etc. I have modified the controller like this:

def show(conn, %{"id" => id}) do
  my_model = Repo.get!(MyModel, id)
  case get_format(conn) do
    "json" ->
      render(conn, my_model: my_model)
    _ ->
      render(conn, "show.html", my_model: my_model)
  end
end

along with the view:

defmodule MyProject.MyModelView do
  use Laired.Web, :view

  def render("show.json", %{my_model: my_model}) do
    my_model
  end
end

Related:

Setting up custom response for exception in Phoenix Application

like image 700
Josh Petitt Avatar asked Dec 06 '15 01:12

Josh Petitt


2 Answers

Use get instead of get! and handle the logic when it returns nil:

def show(conn,%{"id" => id}) do
  case Repo.get(MyModel, id) do
    nil -> # return null and 404 
    record -> # do something with record        
  end
end  
like image 127
NoDisplayName Avatar answered Sep 20 '22 00:09

NoDisplayName


Can catch the error using try, rescue as well

def show(conn,%{"id" => id}) do
    try do
      result =
        Repo.get!(MyModel, id)

      {:ok, result}
    rescue
      Ecto.NoResultsError ->
        {:error, :not_found, "No result found"}
    end
end
like image 21
Sajal Sharma Avatar answered Sep 21 '22 00:09

Sajal Sharma