Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Remove code duplication for Rack middleware #272

Open
wants to merge 1 commit into from

1 participant

@omerisimo

Browsing through the code in an attempt to understand the logic for sending notifications I found that Airbrake::Rack and Airbrake::Rails::Middleware classes had a lot of common logic.
This made the code hard to follow and understand the differences between the two implementations.

This pull request makes the code of the two classes DRY :) [Don't Repeat Yourself]
All tests are passing, and I have performed some manual testing to make sure the full integration works well in both Rails 3.2 and Sinatra.

@omerisimo omerisimo Remove code duplication for Rack middleware
Airbrake::Rack and Airbrake::Rails::Middleware code is now DRY
10f7feb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 26, 2014
  1. @omerisimo

    Remove code duplication for Rack middleware

    omerisimo authored
    Airbrake::Rack and Airbrake::Rails::Middleware code is now DRY
This page is out of date. Refresh to see the latest.
Showing with 32 additions and 50 deletions.
  1. +27 −14 lib/airbrake/rack.rb
  2. +5 −36 lib/airbrake/rails/middleware.rb
View
41 lib/airbrake/rack.rb
@@ -25,20 +25,6 @@ def initialize(app)
Airbrake.configuration.framework = "Rack: #{::Rack.release}"
end
- def ignored_user_agent?(env)
- true if Airbrake.
- configuration.
- ignore_user_agent.
- flatten.
- any? { |ua| ua === env['HTTP_USER_AGENT'] }
- end
-
- def notify_airbrake(exception, env)
- unless ignored_user_agent?(env)
- Airbrake.notify_or_ignore(exception, :rack_env => env)
- end
- end
-
def call(env)
begin
response = @app.call(env)
@@ -54,9 +40,36 @@ def call(env)
response
end
+ protected
+
def framework_exception(env)
env['rack.exception'] || env['sinatra.error']
end
+ def request_data(env)
+ {:rack_env => env}
+ end
+
+ def after_airbrake_handler(env, exception)
+ # Do nothing
+ end
+
+ private
+
+ def ignored_user_agent?(env)
+ true if Airbrake.
+ configuration.
+ ignore_user_agent.
+ flatten.
+ any? { |ua| ua === env['HTTP_USER_AGENT'] }
+ end
+
+ def notify_airbrake(exception, env)
+ unless ignored_user_agent? env
+ error_id = Airbrake.notify_or_ignore(exception, request_data(env))
+ after_airbrake_handler(env, exception)
+ error_id
+ end
+ end
end
end
View
41 lib/airbrake/rails/middleware.rb
@@ -3,27 +3,9 @@ module Rails
# Rack middleware for Rails applications. Any errors raised by the upstream
# application will be delivered to Airbrake and re-raised.
#
- class Middleware
- def initialize(app)
- @app = app
- end
-
- def call(env)
- begin
- response = @app.call(env)
- rescue Exception => exception
- env['airbrake.error_id'] = notify_airbrake(env, exception)
- raise exception
- end
-
- if framework_exception = env["action_dispatch.exception"]
- env["airbrake.error_id"] = notify_airbrake(env, framework_exception)
- end
+ class Middleware < Airbrake::Rack
- response
- end
-
- private
+ protected
def after_airbrake_handler(env, exception)
if defined? env["action_controller.instance"].
@@ -34,25 +16,12 @@ def after_airbrake_handler(env, exception)
end
end
- def notify_airbrake(env, exception)
- unless ignored_user_agent? env
- error_id = Airbrake.notify_or_ignore(exception, request_data(env))
- after_airbrake_handler(env, exception)
- error_id
- end
- end
-
def request_data(env)
- env["action_controller.instance"].try(:airbrake_request_data) ||
- {:rack_env => env}
+ env["action_controller.instance"].try(:airbrake_request_data) || super
end
- def ignored_user_agent?(env)
- true if Airbrake.
- configuration.
- ignore_user_agent.
- flatten.
- any? { |ua| ua === env['HTTP_USER_AGENT'] }
+ def framework_exception(env)
+ env["action_dispatch.exception"]
end
end
end
Something went wrong with that request. Please try again.