Allow 404 error handler to be customizable
This commit is contained in:
parent
92d7273759
commit
67abb5fff3
3 changed files with 9 additions and 8 deletions
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# Template for 404 Not Found
|
||||
def render_404(context)
|
||||
def render_404
|
||||
template = <<-HTML
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
@ -16,10 +16,6 @@ def render_404(context)
|
|||
</body>
|
||||
</html>
|
||||
HTML
|
||||
context.response.content_type = "text/html"
|
||||
context.response.status_code = 404
|
||||
context.response.print template
|
||||
context
|
||||
end
|
||||
|
||||
# Template for 500 Internal Server Error
|
||||
|
|
Loading…
Reference in a new issue