Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Move error_handler onto config.robot.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed Mar 13, 2015
1 parent a566bcf commit 1f6a3f6
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 10 deletions.
4 changes: 0 additions & 4 deletions lib/lita.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,8 +80,6 @@ def run(config_path = nil)
Robot.new.run
end
end

self.error_handler = -> (_error) {}
end

require_relative "lita/version"
Expand Down
5 changes: 5 additions & 0 deletions lib/lita/default_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/lita/handler/chat_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/lita/http_callback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lita/handler/chat_router_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lita/handler/http_router_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1f6a3f6

Please sign in to comment.