Class: BetterErrors::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/better_errors/middleware.rb

Overview

Better Errors' error handling middleware. Including this in your middleware stack will show a Better Errors error page for exceptions raised below this middleware.

If you are using Ruby on Rails, you do not need to manually insert this middleware into your middleware stack.

Examples:

Sinatra

require "better_errors"

if development?
  use BetterErrors::Middleware
end

Rack

require "better_errors"
if ENV["RACK_ENV"] == "development"
  use BetterErrors::Middleware
end

Constant Summary collapse

ALLOWED_IPS =

The set of IP addresses that are allowed to access Better Errors.

Set to { "127.0.0.1/8", "::1/128" } by default.

Set.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, handler = ErrorPage) ⇒ Middleware

A new instance of BetterErrors::Middleware

Parameters:

  • app

    The Rack app/middleware to wrap with Better Errors

  • handler (defaults to: ErrorPage)

    The error handler to use.


45
46
47
48
# File 'lib/better_errors/middleware.rb', line 45

def initialize(app, handler = ErrorPage)
  @app      = app
  @handler  = handler
end

Class Method Details

.allow_ip!(addr) ⇒ Object

Adds an address to the set of IP addresses allowed to access Better Errors.


34
35
36
# File 'lib/better_errors/middleware.rb', line 34

def self.allow_ip!(addr)
  ALLOWED_IPS << IPAddr.new(addr)
end

Instance Method Details

#call(env) ⇒ Array

Calls the Better Errors middleware

Parameters:

  • env (Hash)

Returns:

  • (Array)

54
55
56
57
58
59
60
# File 'lib/better_errors/middleware.rb', line 54

def call(env)
  if allow_ip? env
    better_errors_call env
  else
    @app.call env
  end
end