Class: Bugsnag::MiddlewareStack

Inherits:
Object
  • Object
show all
Defined in:
lib/bugsnag/middleware_stack.rb

Instance Method Summary collapse

Constructor Details

#initializeMiddlewareStack

Returns a new instance of MiddlewareStack.



3
4
5
6
# File 'lib/bugsnag/middleware_stack.rb', line 3

def initialize
  @middlewares = []
  @disabled_middleware = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

This allows people to proxy methods to the array if they want to do more complex stuff



39
40
41
# File 'lib/bugsnag/middleware_stack.rb', line 39

def method_missing(method, *args, &block)
  @middlewares.send(method, *args, &block)
end

Instance Method Details

#disable(*middlewares) ⇒ Object



32
33
34
35
36
# File 'lib/bugsnag/middleware_stack.rb', line 32

def disable(*middlewares)
  @disabled_middleware += middlewares

  @middlewares.delete_if {|m| @disabled_middleware.include?(m)}
end

#insert_after(after, new_middleware) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/bugsnag/middleware_stack.rb', line 14

def insert_after(after, new_middleware)
  return if @disabled_middleware.include?(new_middleware)

  index = @middlewares.rindex(after)
  if index.nil?
    @middlewares << new_middleware
  else
    @middlewares.insert index + 1, new_middleware
  end
end

#insert_before(before, new_middleware) ⇒ Object



25
26
27
28
29
30
# File 'lib/bugsnag/middleware_stack.rb', line 25

def insert_before(before, new_middleware)
  return if @disabled_middleware.include?(new_middleware)

  index = @middlewares.index(before) || @middlewares.length
  @middlewares.insert index, new_middleware
end

#run(notification) ⇒ Object

Runs the middleware stack and calls



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bugsnag/middleware_stack.rb', line 44

def run(notification)
  # The final lambda is the termination of the middleware stack. It calls deliver on the notification
  lambda_has_run = false
  notify_lambda = lambda do |notification|
    lambda_has_run = true        
    yield
  end

  begin
    # We reverse them, so we can call "call" on the first middleware
    middleware_procs.reverse.inject(notify_lambda) { |n,e| e[n] }.call(notification)
  rescue StandardError => e
    # KLUDGE: Since we don't re-raise middleware exceptions, this breaks rspec
    raise if e.class.to_s == "RSpec::Expectations::ExpectationNotMetError"

    # We dont notify, as we dont want to loop forever in the case of really broken middleware, we will
    # still send this notify
    Bugsnag.warn "Bugsnag middleware error: #{e}"
    Bugsnag.log "Middleware error stacktrace: #{e.backtrace.inspect}"
  end
  
  # Ensure that the deliver has been performed, and no middleware has botched it
  notify_lambda.call(notification) unless lambda_has_run
end

#use(new_middleware) ⇒ Object



8
9
10
11
12
# File 'lib/bugsnag/middleware_stack.rb', line 8

def use(new_middleware)
  return if @disabled_middleware.include?(new_middleware)

  @middlewares << new_middleware
end