Class: Temescal::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) {|configuration| ... } ⇒ Middleware

Public: Initializes the middleware.

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

Returns an instance of the middleware.

Yields:

  • (configuration)


9
10
11
12
# File 'lib/temescal/middleware.rb', line 9

def initialize(app, &block)
  @app = app
  yield(configuration) if block
end

Instance Method Details

#call(env) ⇒ Object

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.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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