Current section

Files

Jump to
raygun lib raygun plug.ex
Raw

lib/raygun/plug.ex

defmodule Raygun.Plug.State do
def start_link do
Agent.start_link(fn -> nil end, name: __MODULE__)
end
def set(value) do
Agent.update(__MODULE__, fn (_x) -> value end)
end
def get do
Agent.get(__MODULE__, fn(x) -> x end)
end
end
defmodule Raygun.Plug do
@moduledoc """
This plug is designed to wrap calls in a router (commonly in Phoenix) so that
any exceptions will be sent to Raygun.
"""
defmacro __using__(_env) do
quote location: :keep do
@before_compile Raygun.Plug
end
end
@doc """
Whenever an error occurs, capture the stacktrace and exception to send to Raygun.
"""
defmacro __before_compile__(env) do
quote location: :keep do
defoverridable [call: 2]
Raygun.Plug.State.start_link
Raygun.Plug.State.set(unquote(env))
def call(conn, opts) do
try do
super(conn, opts)
rescue
exception ->
stacktrace = System.stacktrace
env = Raygun.Plug.State.get()
IO.inspect env
user = if env do
env.user(conn)
end
IO.puts "user is #{user}"
Raygun.report_plug(conn, stacktrace, exception, env: Atom.to_string(Mix.env), user: user)
reraise exception, stacktrace
end
end
end
end
end