diff options
author | Genadi Samokovarov <gsamokovarov@gmail.com> | 2016-02-24 23:19:57 +0200 |
---|---|---|
committer | Genadi Samokovarov <gsamokovarov@gmail.com> | 2018-04-20 14:55:37 +0300 |
commit | d25fba89d49bf741de6d7dfa857afcc61468b815 (patch) | |
tree | a4f60efac23ce6fbefc81dabe9c14e2ed9b8e7ea /actionpack/lib/action_dispatch/middleware | |
parent | 1694b02909850c9d8f47fb644e98e1203d9f2898 (diff) | |
download | rails-d25fba89d49bf741de6d7dfa857afcc61468b815.tar.gz rails-d25fba89d49bf741de6d7dfa857afcc61468b815.tar.bz2 rails-d25fba89d49bf741de6d7dfa857afcc61468b815.zip |
Introduce ActionDispatch::DebugExceptions interceptors
Plugins interacting with the exceptions caught and displayed by
ActionDispatch::DebugExceptions currently have to monkey patch it to get
the much needed exception for their calculation.
With DebugExceptions.register_interceptor, plugin authors can hook into
DebugExceptions and process the exception, before being rendered. They
can store it into the request and process it on the way back of the
middleware chain execution or act on it straight in the interceptor.
The interceptors can be play blocks, procs, lambdas or any object that
responds to `#call`.
Diffstat (limited to 'actionpack/lib/action_dispatch/middleware')
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/debug_exceptions.rb | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb index 511306eb0e..33edad8bd9 100644 --- a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb @@ -50,10 +50,18 @@ module ActionDispatch end end - def initialize(app, routes_app = nil, response_format = :default) + cattr_reader :interceptors, instance_accessor: false, default: [] + + def self.register_interceptor(object = nil, &block) + interceptor = object || block + interceptors << interceptor + end + + def initialize(app, routes_app = nil, response_format = :default, interceptors = self.class.interceptors) @app = app @routes_app = routes_app @response_format = response_format + @interceptors = interceptors end def call(env) @@ -67,12 +75,26 @@ module ActionDispatch response rescue Exception => exception + invoke_interceptors(request, exception) raise exception unless request.show_exceptions? render_exception(request, exception) end private + def invoke_interceptors(request, exception) + backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner") + wrapper = ExceptionWrapper.new(backtrace_cleaner, exception) + + @interceptors.each do |interceptor| + begin + interceptor.call(request, exception) + rescue Exception + log_error(request, wrapper) + end + end + end + def render_exception(request, exception) backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner") wrapper = ExceptionWrapper.new(backtrace_cleaner, exception) |