Skip to content

Commit

Permalink
Capture bodies of raised NotFound and BadRequest errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mwpastore committed Nov 29, 2016
1 parent 63e81bc commit fd8ddc9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1137,15 +1137,16 @@ def handle_exception!(boom)

status(500) unless status.between? 400, 599

boom_body = boom.message if boom.message && boom.message != boom.class.name
if server_error?
dump_errors! boom if settings.dump_errors?
raise boom if settings.show_exceptions? and settings.show_exceptions != :after_handler
elsif not_found?
headers['X-Cascade'] = 'pass' if settings.x_cascade?
body '<h1>Not Found</h1>'
body boom_body || '<h1>Not Found</h1>'
elsif bad_request?
dump_errors! boom if settings.dump_errors?
halt status
halt status, boom_body
end

res = error_block!(boom.class, boom) || error_block!(status, boom)
Expand Down
26 changes: 26 additions & 0 deletions test/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,32 @@ class RoutingTest < Minitest::Test
assert_equal 404, status
end

it "captures the body of a raised NotFound" do
mock_app {
get '/' do
raise Sinatra::NotFound, "This is not a drill"
end
}

get "/"
assert_equal "19", response["Content-Length"]
assert_equal 404, status
assert_equal "This is not a drill", response.body
end

it "captures the body of a raised BadRequest" do
mock_app {
get '/' do
raise Sinatra::BadRequest, "This is not a drill either"
end
}

get "/"
assert_equal "26", response["Content-Length"]
assert_equal 400, status
assert_equal "This is not a drill either", response.body
end

it "uses 404 error handler for not matching route" do
mock_app {
not_found do
Expand Down

0 comments on commit fd8ddc9

Please sign in to comment.