diff --git a/spec/common_exception_handler_spec.cr b/spec/common_exception_handler_spec.cr index e7bdd1b..e531134 100644 --- a/spec/common_exception_handler_spec.cr +++ b/spec/common_exception_handler_spec.cr @@ -34,11 +34,12 @@ describe "Kemal::CommonExceptionHandler" do io.rewind response = HTTP::Client::Response.from_io(io, decompress: false) response.status_code.should eq 403 + response.headers["Content-Type"].should eq "text/html" response.body.should eq "403 error" end it "renders custom 500 error" do - error 500 do + error 500 do |env| "Something happened" end get "/" do |env| @@ -54,6 +55,29 @@ describe "Kemal::CommonExceptionHandler" do io.rewind response = HTTP::Client::Response.from_io(io, decompress: false) response.status_code.should eq 500 + response.headers["Content-Type"].should eq "text/html" + response.body.should eq "Something happened" + end + + it "keeps the specified error Content-Type" do + error 500 do |env| + "Something happened" + end + get "/" do |env| + env.response.content_type = "application/json" + env.response.status_code = 500 + end + request = HTTP::Request.new("GET", "/") + io = MemoryIO.new + response = HTTP::Server::Response.new(io) + context = HTTP::Server::Context.new(request, response) + Kemal::CommonExceptionHandler::INSTANCE.next = Kemal::RouteHandler::INSTANCE + Kemal::CommonExceptionHandler::INSTANCE.call(context) + response.close + io.rewind + response = HTTP::Client::Response.from_io(io, decompress: false) + response.status_code.should eq 500 + response.headers["Content-Type"].should eq "application/json" response.body.should eq "Something happened" end end diff --git a/src/kemal/common_exception_handler.cr b/src/kemal/common_exception_handler.cr index 1ca895c..bebde04 100644 --- a/src/kemal/common_exception_handler.cr +++ b/src/kemal/common_exception_handler.cr @@ -20,9 +20,9 @@ module Kemal def call_exception_with_status_code(context, status_code) if Kemal.config.error_handlers.has_key?(status_code) - context.response.status_code = status_code + context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type") context.response.print Kemal.config.error_handlers[status_code].call(context) - return context + context end end end