How to call fallback controller on exceptions to send custom response

Is there any standard way to catch the exception in controllers and send a custom response to the client as the fallback controller does not get called on exceptions

action_fallback SocketEngineWeb.FallbackController

def get_online_status(conn, %{"userIds" => user_ids} = params) do
    try do
      {:ok, online_status} = user_ids |> Jason.decode!() |> ChatTracker.get_online_status()
      conn |> send_response(200, "status fetched", %{"status" => online_status})
    rescue
      err -> err
    end
  end

Exceptions are handled by the Plug.Exceptions protocol and not with the action fallback.

2 Likes

Thanks for your response.
Can you tell how to send a custom response on any exception and does it mean I should not use try and rescue to catch exception to call send response function by myself.

http://joshwlewis.com/essays/elixir-error-handling-with-plug/ This explains how to work with Plug.Exception