class Temescal::Middleware

Public Class Methods

new(app) { |configuration| ... } click to toggle source

Public: Initializes the middleware.

app - The Rack application. block - Optional block for configuring the middleware.

Returns an instance of the middleware.

# File lib/temescal/middleware.rb, line 9
def initialize(app, &block)
  @app = app
  yield(configuration) if block
end

Public Instance Methods

call(env) click to toggle source

Public: call method for Rack application. Rescues from an exception and does the following: 1) Logs the error 2) Reports error to configured monitoring services 3) Generates a JSON response with error information.

env - The environment of the request.

Returns an array of response data for Rack.

# File lib/temescal/middleware.rb, line 21
def call(env)
  begin
    @status, @headers, @response = @app.call(env)
  rescue => exception
    raise if configuration.raise_errors?

    error = Error.new(exception)

    unless error.ignore?
      $stderr.print error.formatted
      configuration.monitors.each { |monitor| monitor.report(exception) }
    end

    @status   = error.status
    @response = Response.build(error)
    @headers  = { "Content-Type" => "application/json" }
  end
  [@status, @headers, @response]
end

Private Instance Methods

configuration() click to toggle source

Private: Getter for Temescal configuration.

# File lib/temescal/middleware.rb, line 44
def configuration
  $_temescal_configuration ||= Configuration.new
end