Allow 404 error handler to be customizable

This commit is contained in:
Sdogruyol 2016-05-15 14:58:09 +03:00
parent 92d7273759
commit 67abb5fff3
3 changed files with 9 additions and 8 deletions

View file

@ -12,8 +12,10 @@ module Kemal
config.server = HTTP::Server.new(config.host_binding.not_nil!, config.port, config.handlers) config.server = HTTP::Server.new(config.host_binding.not_nil!, config.port, config.handlers)
config.server.not_nil!.ssl = config.ssl config.server.not_nil!.ssl = config.ssl
error 404 do |env| unless Kemal.config.error_handlers.has_key?(404)
render_404(env) error 404 do |env|
render_404
end
end end
# Test environment doesn't need to have signal trap, built-in images, and logging. # Test environment doesn't need to have signal trap, built-in images, and logging.

View file

@ -6,7 +6,10 @@ module Kemal
begin begin
call_next(context) call_next(context)
rescue Kemal::Exceptions::RouteNotFound rescue Kemal::Exceptions::RouteNotFound
return Kemal.config.error_handlers[404].call(context) context.response.content_type = "text/html"
context.response.status_code = 404
context.response.print Kemal.config.error_handlers[404].call(context)
return context
rescue Kemal::Exceptions::CustomException rescue Kemal::Exceptions::CustomException
status_code = context.response.status_code status_code = context.response.status_code
if Kemal.config.error_handlers.has_key?(status_code) if Kemal.config.error_handlers.has_key?(status_code)

View file

@ -1,5 +1,5 @@
# Template for 404 Not Found # Template for 404 Not Found
def render_404(context) def render_404
template = <<-HTML template = <<-HTML
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
@ -16,10 +16,6 @@ def render_404(context)
</body> </body>
</html> </html>
HTML HTML
context.response.content_type = "text/html"
context.response.status_code = 404
context.response.print template
context
end end
# Template for 500 Internal Server Error # Template for 500 Internal Server Error