diff --git a/lib/lita.rb b/lib/lita.rb index c4a5ddc9..0f18ae10 100644 --- a/lib/lita.rb +++ b/lib/lita.rb @@ -37,8 +37,6 @@ class << self attr_accessor :test_mode alias_method :test_mode?, :test_mode - attr_accessor :error_handler - # The global Logger object. # @return [::Logger] The global Logger object. def logger @@ -82,8 +80,6 @@ def run(config_path = nil) Robot.new.run end end - - self.error_handler = -> (_error) {} end require_relative "lita/version" diff --git a/lib/lita/default_configuration.rb b/lib/lita/default_configuration.rb index 6942ab36..649ee394 100644 --- a/lib/lita/default_configuration.rb +++ b/lib/lita/default_configuration.rb @@ -139,6 +139,11 @@ def robot_config end end config :admins + config :error_handler, default: -> (_error) {} do + validate do |value| + "must respond to #call" unless value.respond_to?(:call) + end + end end end end diff --git a/lib/lita/handler/chat_router.rb b/lib/lita/handler/chat_router.rb index 07b6d9f0..6fc9662d 100644 --- a/lib/lita/handler/chat_router.rb +++ b/lib/lita/handler/chat_router.rb @@ -88,7 +88,7 @@ def dispatch_to_route(route, robot, message) route.callback.call(handler, response) rescue Exception => e log_dispatch_error(e) - Lita.error_handler.call(e) + robot.config.robot.error_handler.call(e) raise e if Lita.test_mode? end diff --git a/lib/lita/http_callback.rb b/lib/lita/http_callback.rb index 01f0b73b..913678a7 100644 --- a/lib/lita/http_callback.rb +++ b/lib/lita/http_callback.rb @@ -23,7 +23,7 @@ def call(env) @callback.call(handler, request, response) rescue Exception => e - Lita.error_handler.call(e) + env["lita.robot"].config.robot.error_handler.call(e) raise end end diff --git a/spec/lita/handler/chat_router_spec.rb b/spec/lita/handler/chat_router_spec.rb index acfcefcc..2d38e9a6 100644 --- a/spec/lita/handler/chat_router_spec.rb +++ b/spec/lita/handler/chat_router_spec.rb @@ -193,8 +193,8 @@ def self.name end context "when the handler raises an exception" do - it "calls Lita.error_handler with the exception as argument" do - expect(Lita.error_handler).to receive(:call).with(instance_of(TypeError)) + it "calls the error handler with the exception as argument" do + expect(registry.config.robot.error_handler).to receive(:call).with(instance_of(TypeError)) expect { send_message("boom!") }.to raise_error(TypeError) end diff --git a/spec/lita/handler/http_router_spec.rb b/spec/lita/handler/http_router_spec.rb index 8ea8047b..85e823b9 100644 --- a/spec/lita/handler/http_router_spec.rb +++ b/spec/lita/handler/http_router_spec.rb @@ -89,8 +89,8 @@ def boom(_request, _response) end context "when the handler raises an exception" do - it "calls Lita.error_handler with the exception as argument" do - expect(Lita.error_handler).to receive(:call).with(instance_of(TypeError)) + it "calls the error handler with the exception as argument" do + expect(registry.config.robot.error_handler).to receive(:call).with(instance_of(TypeError)) expect { http.get("/boom") }.to raise_error(TypeError, "String can't be coerced into Fixnum") end