Plug.Debugger

A module (not a plug) for debugging in development.

This module is commonly used within a Plug.Builder or a Plug.Router and it wraps the call/2 function.

Notice Plug.Debugger does not catch errors, as errors should still propagate so that the Elixir process finishes with the proper reason. This module does not perform any logging either, as all logging is done by the web server handler.

Note: If this module is used with Plug.ErrorHandler, it must be used before Plug.ErrorHandler.

Examples

defmodule MyApp do
  use Plug.Builder

  if Mix.env == :dev do
    use Plug.Debugger, otp_app: :my_app
  end

  plug :boom

  def boom(conn, _) do
    # Error raised here will be caught and displayed in a debug page
    # complete with a stacktrace and other helpful infos.
    raise "oops"
  end
end

Options

Links to the text editor

If a PLUG_EDITOR environment variable is set, Plug.Debugger is going to use it to generate links to your text editor. The variable should be set with __FILE__ and __LINE__ placeholders which will be correctly replaced. For example (with the TextMate editor):

txmt://open/?url=file://__FILE__&line=__LINE__

Manual usage

Plug.Debugger can also be used manually by invoking the wrap/3 function directly. For example:

defmodule Plt do
  use Plug.Builder
  # No `use Plug.Builder` here.

  plug :bomb

  def bomb(conn, _opts) do
    Plug.Debugger.wrap conn, [otp_app: :my_app], fn ->
      raise "this exception will be wrapped!"
    end
  end
end

Summary

wrap(conn, opts, fun)

Wraps a given function fun and renders a nice error page in case the function fails. The page is rendered by setting it as the body of conn, which is then sent. Because the connection is sent, no further plugs in the plug pipeline can modify it

Functions

wrap(conn, opts, fun)

Specs:

Wraps a given function fun and renders a nice error page in case the function fails. The page is rendered by setting it as the body of conn, which is then sent. Because the connection is sent, no further plugs in the plug pipeline can modify it.

Options

  • :otp_app - the name of the OTP application considered to be the main application

Examples

dangerous_fun = fn -> raise "told ya!" end
Plug.Debugger.wrap(conn, [otp_app: :my_app], dangerous_fun)