Ecto.NoResultsError was not handled in phoenix_ecto

I’m using Repo.get_by! function for fetching a specific resource, when resource not found it will throw Ecto.NoResultsError exception.

[info] Sent 404 in 7ms
[debug] ** (Ecto.NoResultsError) expected at least one result but got none in query:

from s0 in Senti.Presentations.Session,
  where: s0.access_code == ^"1"

    (ecto 3.6.1) lib/ecto/repo/queryable.ex:161: Ecto.Repo.Queryable.one!/3
    (senti 0.1.0) lib/senti_web/controllers/presentation_controller.ex:52: SentiWeb.PresentationController.join/2
    (senti 0.1.0) lib/senti_web/controllers/presentation_controller.ex:1: SentiWeb.PresentationController.action/2
    (senti 0.1.0) lib/senti_web/controllers/presentation_controller.ex:1: SentiWeb.PresentationController.phoenix_controller_pipeline/2
    (phoenix 1.5.8) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
    (senti 0.1.0) lib/senti_web/endpoint.ex:1: SentiWeb.Endpoint.plug_builder_call/2
    (senti 0.1.0) lib/plug/debugger.ex:136: SentiWeb.Endpoint."call (overridable 3)"/2
    (senti 0.1.0) lib/senti_web/endpoint.ex:1: SentiWeb.Endpoint.call/2
    (phoenix 1.5.8) lib/phoenix/endpoint/cowboy2_handler.ex:65: Phoenix.Endpoint.Cowboy2Handler.init/4
    (cowboy 2.8.0) /home/hei/projects/senti/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
    (cowboy 2.8.0) /home/hei/projects/senti/deps/cowboy/src/cowboy_stream_h.erl:300: :cowboy_stream_h.execute/3
    (cowboy 2.8.0) /home/hei/projects/senti/deps/cowboy/src/cowboy_stream_h.erl:291: :cowboy_stream_h.request_process/3
    (stdlib 3.14.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

I can make the FallbackController handle when resource not found by modifying the code by using Repo.get_by and return either {:ok, resource} or {:error, :not_found}.

My question is does Phoenix_ecto handles it automatically? Based on this code, it was handled already.

Please correct me if I misunderstood it.

Thank you.

1 Like