Skip to content

Commit

Permalink
Handle endpoint exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
crbelaus committed Dec 15, 2024
1 parent 64bd2c0 commit bd32d51
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ defmodule ErrorTrackerDev.Live do
<li>
<.link href="/exception">Generate an exception from the controller</.link>
</li>
<li>
<.link href="/plug_exception">Generate an exception from the router</.link>
</li>
<li>
<.link href="/exit">Generate an exit from the controller</.link>
</li>
Expand Down Expand Up @@ -205,6 +208,8 @@ defmodule ErrorTrackerDev.Endpoint do

# Use a custom Content Security Policy
plug :set_csp
# Raise an exception in the /plug_exception path
plug :plug_exception
# Our custom router which allows us to have regular controllers and live views
plug ErrorTrackerDev.Router

Expand All @@ -220,6 +225,12 @@ defmodule ErrorTrackerDev.Endpoint do
|> Plug.Conn.assign(:custom_csp_nonce, "#{nonce}")
|> Plug.Conn.put_resp_header("content-security-policy", Enum.join(policies, " "))
end

defp plug_exception(conn = %Plug.Conn{path_info: path_info}, _opts) when is_list(path_info) do
if "plug_exception" in path_info,
do: raise("Crashed in Endpoint"),
else: conn
end
end

defmodule ErrorTrackerDev.ErrorView do
Expand Down

0 comments on commit bd32d51

Please sign in to comment.