Packages
honeybadger
0.14.1
0.28.0
0.27.0
0.26.0
0.25.0
0.24.1
0.24.0
0.23.0
0.22.1
0.22.0
0.21.0
0.20.0
0.19.0
0.18.1
0.18.0
0.17.0
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.0
0.14.1
0.14.0
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.3
0.10.2
0.10.1
0.10.0
0.9.0
0.8.0
0.7.0
0.7.0-beta
0.6.3
0.6.2
0.6.1
0.6.0
0.5.0
0.4.0
0.3.1
0.3.0
0.2.0
0.1.2
0.1.1
0.1.0
Elixir client, Plug and error_logger for integrating with the Honeybadger.io exception tracker"
Current section
Files
Jump to
Current section
Files
lib/honeybadger/plug.ex
if Code.ensure_loaded?(Plug) do
defmodule Honeybadger.Plug do
@moduledoc """
The `Honeybadger.Plug` adds automatic error handling to a plug pipeline.
Within a `Plug.Router` or `Phoenix.Router` use the module and crashes will
be reported to Honeybadger. It's best to `use Honeybadger.Plug` **after
the Router plugs** so that exceptions due to non-matching routes are not
reported to Honeybadger.
### Example
defmodule MyPhoenixApp.Router do
use Crywolf.Web, :router
use Honeybadger.Plug
pipeline :browser do
[...]
end
end
## Customizing
Data reporting may be customized by passing an alterate `:plug_data`
module. This is useful when working with alternate frameworks, such as
Absinthe for GraphQL APIs.
Any module with a `metadata/2` function that accepts a `Plug.Conn` and a
`module` name can be used to generate metadata.
### Example
defmodule MyPhoenixApp.Router do
use Crywolf.Web, :router
use Honeybadger.Plug, plug_data: MyAbsinthePlugData
end
"""
alias Honeybadger.PlugData
alias Honeybadger.Breadcrumbs.{Breadcrumb, Collector}
@doc false
defmacro __using__(opts) do
quote location: :keep do
use Plug.ErrorHandler
@plug_data Keyword.get(unquote(opts), :plug_data, PlugData)
@doc """
Called by `Plug.ErrorHandler` when an error is caught.
By default this ignores "Not Found" errors for `Plug` or `Phoenix`
pipelines. It may be overridden to ignore additional errors or to
customize the data that is used for notifications.
"""
@spec handle_errors(Plug.Conn.t(), %{kind: atom(), reason: any(), stack: any()}) :: :ok
def handle_errors(_conn, %{reason: %FunctionClauseError{function: :do_match}}), do: :ok
def handle_errors(conn, %{reason: reason, stack: stack}) do
if Plug.Exception.status(reason) == 404 do
# 404 errors are not reported
:ok
else
Collector.add(Breadcrumb.from_error(reason))
metadata = @plug_data.metadata(conn, __MODULE__)
Honeybadger.notify(reason, metadata, stack)
end
end
defoverridable handle_errors: 2
end
end
end
end