From 67abb5fff382c67cdd28f3f5f4a694d5d1418c2b Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Sun, 15 May 2016 14:58:09 +0300 Subject: [PATCH] Allow 404 error handler to be customizable --- src/kemal.cr | 6 ++++-- src/kemal/common_exception_handler.cr | 5 ++++- src/kemal/view.cr | 6 +----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/kemal.cr b/src/kemal.cr index 62beb49..641ce2e 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -12,8 +12,10 @@ module Kemal config.server = HTTP::Server.new(config.host_binding.not_nil!, config.port, config.handlers) config.server.not_nil!.ssl = config.ssl - error 404 do |env| - render_404(env) + unless Kemal.config.error_handlers.has_key?(404) + error 404 do |env| + render_404 + end end # Test environment doesn't need to have signal trap, built-in images, and logging. diff --git a/src/kemal/common_exception_handler.cr b/src/kemal/common_exception_handler.cr index 6321f38..8d4bccf 100644 --- a/src/kemal/common_exception_handler.cr +++ b/src/kemal/common_exception_handler.cr @@ -6,7 +6,10 @@ module Kemal begin call_next(context) 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 status_code = context.response.status_code if Kemal.config.error_handlers.has_key?(status_code) diff --git a/src/kemal/view.cr b/src/kemal/view.cr index f4caad4..4062abd 100644 --- a/src/kemal/view.cr +++ b/src/kemal/view.cr @@ -1,5 +1,5 @@ # Template for 404 Not Found -def render_404(context) +def render_404 template = <<-HTML @@ -16,10 +16,6 @@ def render_404(context) HTML - context.response.content_type = "text/html" - context.response.status_code = 404 - context.response.print template - context end # Template for 500 Internal Server Error