Class: BugHunter::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
# File 'lib/bug_hunter/middleware.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bug_hunter/middleware.rb', line 7

def call(env)
  response = nil
  begin
    response = @app.call(env)
  rescue StandardError, LoadError, SyntaxError => e
    error = BugHunter::Error.build_from(env, e)

    if !error.valid? && !error.errors[:uniqueness].empty?
      BugHunter::Error.collection.update(error.unique_error_selector,
                                         {:$inc => {:times => 1}, :$set => {:updated_at => Time.now.utc}},
                                         {:multi => true})
    else
      error.save
    end

    raise e
  end

  response
end