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

Commit

Permalink
Merge pull request #83 from byroot/error-reporting-hook
Browse files Browse the repository at this point in the history
Allow to register a error handler
  • Loading branch information
jimmycuadra committed Mar 6, 2015
2 parents b7ab277 + 19ad471 commit f4fa083
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/lita.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ 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 @@ -80,6 +82,8 @@ def run(config_path = nil)
Robot.new.run
end
end

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

require_relative "lita/version"
Expand Down
1 change: 1 addition & 0 deletions lib/lita/handler/chat_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +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)
raise e if Lita.test_mode?
end

Expand Down
9 changes: 7 additions & 2 deletions lib/lita/http_callback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ def call(env)
if request.head?
response.status = 204
else
handler = @handler_class.new(env["lita.robot"])
begin
handler = @handler_class.new(env["lita.robot"])

@callback.call(handler, request, response)
@callback.call(handler, request, response)
rescue Exception => e
Lita.error_handler.call(e)
raise
end
end

response.finish
Expand Down
10 changes: 10 additions & 0 deletions spec/lita/handler/chat_router_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def self.name
"Test"
end

route(/boom/) { |_response| 1 + "2" }

route(/one/) { |response| response.reply "got one" }
route(/two/) { |response| response.reply "got two" }

Expand Down Expand Up @@ -189,4 +191,12 @@ def self.name
expect(replies.last).to eq("got three")
end
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))

expect { send_message("boom!") }.to raise_error(TypeError)
end
end
end
13 changes: 13 additions & 0 deletions spec/lita/handler/http_router_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
http.get ":var/otherwise/identical/path", :no_constraint
http.get("block") { |_request, response| response.write("block") }
http.get "middleware", :middleware
http.get "boom", :boom

def web(_request, response)
response.write("it worked")
Expand Down Expand Up @@ -40,6 +41,10 @@ def middleware(request, response)
response["Custom-Header"] = request.env["header_value"] if request.env["use_header"]
response.write("middleware worked") if request.env["custom_rack_middleware_working"]
end

def boom(_request, _response)
1 + "2"
end
end

describe handler, lita_handler: true do
Expand Down Expand Up @@ -82,6 +87,14 @@ def middleware(request, response)
expect(response.status).to eq(200)
expect(response.body).to eq("block")
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))

expect { http.get("/boom") }.to raise_error(TypeError, "String can't be coerced into Fixnum")
end
end
end

describe handler, lita_handler: true do
Expand Down

0 comments on commit f4fa083

Please sign in to comment.