Class: Squash::Rails::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/squash/rails/rack.rb

Overview

Rack middleware that catches exceptions thrown outside the scope of Rails’s request processing. This middleware is automatically added to your stack when you include the ‘squash_rails` gem.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack

Instantiates the middleware.

Parameters:

  • app (Array)

    The middleware stack.



28
29
30
# File 'lib/squash/rails/rack.rb', line 28

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Hash

Rescues any exceptions thrown downstream, notifies Squash, then re-raises them.

Parameters:

  • env (Hash)

    The Rack environment.

Returns:

  • (Hash)

    The Rack result to pass up the stack.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/squash/rails/rack.rb', line 38

def call(env)
  @env = env

  begin
    result = @app.call(env)
  rescue ::Exception => ex
    unless ex.instance_variable_get(:@_squash_controller_notified)
      @env['squash.notified'] = ::Squash::Ruby.notify(ex, squash_rack_data)
    end
    raise ex
  end

  result
end

#filter_for_squash(data, kind) ⇒ Object

This method is abstract.

Override this method to implement filtering of sensitive data in the headers, cookies, and rack hashes (see #squash_rack_data). The method signature is the same as ‘Squash::Ruby#filter_for_squash`, but `kind` can also be `:env` for the Rack environment hash.



60
61
62
# File 'lib/squash/rails/rack.rb', line 60

def filter_for_squash(data, kind)
  data
end

#squash_rack_dataHash<Symbol, Object>

Returns The additional information this middleware gives to ‘Squash::Ruby.notify`.

Returns:

  • (Hash<Symbol, Object>)

    The additional information this middleware gives to ‘Squash::Ruby.notify`.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/squash/rails/rack.rb', line 67

def squash_rack_data
  data = {
      :environment    => environment_name,
      :root           => root_path,

      :headers        => filter_for_squash(request_headers, :headers),
      :request_method => @env['REQUEST_METHOD'].to_s.upcase,
      :schema         => @env['rack.url_scheme'],
      :host           => @env['SERVER_NAME'],
      :port           => @env['SERVER_PORT'],
      :path           => @env['PATH_INFO'],
      :query          => @env['QUERY_STRING'],

      :params         => filter_for_squash(@env['action_dispatch.request.parameters'], :params),
      :session        => filter_for_squash(@env['rack.session'], :session),
      :cookies        => filter_for_squash(@env['rack.request.cookie_hash'], :cookies),

      :"rack.env"     => filter_for_squash(@env, :rack)
  }

  if data[:session]
    flash_key    = defined?(ActionDispatch) ? ActionDispatch::Flash::KEY : 'flash'
    data[:flash] = filter_for_squash(data[:session].delete(flash_key) || {}, :flash)
  end

  data
end