Skip to content

Commit

Permalink
Log unhandled ART::RoutingHandler exceptions (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacksmoke16 authored Nov 29, 2024
1 parent a72d01b commit f852159
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/components/routing/spec/routing_handler_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,24 @@ describe ART::RoutingHandler do
handler = ART::RoutingHandler.new MockURLMatcher.new "foo", ART::Exception::ResourceNotFound.new "Missing"
handler.add("foo", ART::Route.new("/foo")) { }

handler.call HTTP::Server::Context.new HTTP::Request.new("GET", "/foo"), resp = HTTP::Server::Response.new(IO::Memory.new)
Log.capture do |logs|
handler.call HTTP::Server::Context.new HTTP::Request.new("GET", "/foo"), resp = HTTP::Server::Response.new(IO::Memory.new)
resp.status.should eq HTTP::Status::NOT_FOUND

resp.status.should eq HTTP::Status::NOT_FOUND
logs.empty
end
end

it "unsupported method" do
handler = ART::RoutingHandler.new MockURLMatcher.new "foo", ART::Exception::MethodNotAllowed.new ["PUT", "SEARCH"], "Not Allowed"
handler.add("foo", ART::Route.new("/foo")) { }

handler.call HTTP::Server::Context.new HTTP::Request.new("GET", "/foo"), resp = HTTP::Server::Response.new(IO::Memory.new)
Log.capture do |logs|
handler.call HTTP::Server::Context.new HTTP::Request.new("GET", "/foo"), resp = HTTP::Server::Response.new(IO::Memory.new)
resp.status.should eq HTTP::Status::METHOD_NOT_ALLOWED

resp.status.should eq HTTP::Status::METHOD_NOT_ALLOWED
logs.empty
end
end

it "domain exception" do
Expand All @@ -100,9 +106,12 @@ describe ART::RoutingHandler do
raise "Oh no!"
end

handler.call HTTP::Server::Context.new HTTP::Request.new("GET", "/foo"), resp = HTTP::Server::Response.new(IO::Memory.new)
Log.capture do |logs|
handler.call HTTP::Server::Context.new HTTP::Request.new("GET", "/foo"), resp = HTTP::Server::Response.new(IO::Memory.new)
resp.status.should eq HTTP::Status::INTERNAL_SERVER_ERROR

resp.status.should eq HTTP::Status::INTERNAL_SERVER_ERROR
logs.check :error, "Unhandled exception"
end
end
end

Expand Down Expand Up @@ -152,9 +161,12 @@ describe ART::RoutingHandler do
ctx.request.path.should eq "/foo"
raise "Oh no!"
end
Log.capture do |logs|
expect_raises ::Exception, "Oh no!" do
handler.call HTTP::Server::Context.new HTTP::Request.new("GET", "/foo"), HTTP::Server::Response.new(IO::Memory.new)
end

expect_raises ::Exception, "Oh no!" do
handler.call HTTP::Server::Context.new HTTP::Request.new("GET", "/foo"), HTTP::Server::Response.new(IO::Memory.new)
logs.empty
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions src/components/routing/spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ require "spec"
require "athena-spec"
require "../src/athena-routing"

require "log/spec"

ASPEC.run_all

Spec.before_each do
ART::RouteProvider.reset
end

Log.setup :none

# FIXME: Refactor these specs to not depend on calling a protected method.
include Athena::Routing
5 changes: 5 additions & 0 deletions src/components/routing/src/routing_handler.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "log"

# Provides basic routing functionality to an [HTTP::Server](https://crystal-lang.org/api/HTTP/Server.html).
#
# This type works as both a [HTTP::Handler](https://crystal-lang.org/api/HTTP/Handler.html) and
Expand Down Expand Up @@ -64,6 +66,8 @@
class Athena::Routing::RoutingHandler
include HTTP::Handler

private LOG = Log.for "athena.routing"

# :nodoc:
class RouteProvider < ::Athena::Routing::RouteProvider
end
Expand Down Expand Up @@ -113,6 +117,7 @@ class Athena::Routing::RoutingHandler
@handlers[parameters["_route"]].call context, parameters
rescue ex : ::Exception
raise ex if @bubble_exceptions
LOG.error(exception: ex) { "Unhandled exception" }
context.response.respond_with_status(:internal_server_error)
end
end
Expand Down

0 comments on commit f852159

Please sign in to comment.