Merge pull request #515 from kemalcr/fix-closed-io-404

Do not try to call ExceptionHandler#call_exception_with_status_code on a closed response
This commit is contained in:
Serdar Dogruyol 2019-01-17 22:06:36 +03:00 committed by GitHub
commit dcffd7b3f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View file

@ -102,4 +102,14 @@ describe "Kemal::ExceptionHandler" do
response.headers["Content-Type"].should eq "application/json"
response.body.should eq "Rendered error with 500"
end
it "does not do anything on a closed io" do
get "/" do |env|
halt env, status_code: 404
end
request = HTTP::Request.new("GET", "/")
client_response = call_request_on_app(request)
client_response.status_code.should eq 404
end
end

View file

@ -18,6 +18,7 @@ module Kemal
end
private def call_exception_with_status_code(context : HTTP::Server::Context, exception : Exception, status_code : Int32)
return if context.response.closed?
if !Kemal.config.error_handlers.empty? && Kemal.config.error_handlers.has_key?(status_code)
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
context.response.status_code = status_code