mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Yield error in custom error handlers
This commit is contained in:
parent
14b094d52f
commit
012ac8f6b7
5 changed files with 40 additions and 11 deletions
|
@ -6,22 +6,22 @@ module Kemal
|
|||
def call(context)
|
||||
begin
|
||||
call_next(context)
|
||||
rescue Kemal::Exceptions::RouteNotFound
|
||||
call_exception_with_status_code(context, 404)
|
||||
rescue Kemal::Exceptions::CustomException
|
||||
call_exception_with_status_code(context, context.response.status_code)
|
||||
rescue ex : Kemal::Exceptions::RouteNotFound
|
||||
call_exception_with_status_code(context, ex, 404)
|
||||
rescue ex : Kemal::Exceptions::CustomException
|
||||
call_exception_with_status_code(context, ex, context.response.status_code)
|
||||
rescue ex : Exception
|
||||
Kemal.config.logger.write("Exception: #{ex.inspect_with_backtrace}\n")
|
||||
return call_exception_with_status_code(context, 500) if Kemal.config.error_handlers.has_key?(500)
|
||||
return call_exception_with_status_code(context, ex, 500) if Kemal.config.error_handlers.has_key?(500)
|
||||
verbosity = Kemal.config.env == "production" ? false : true
|
||||
return render_500(context, ex.inspect_with_backtrace, verbosity)
|
||||
end
|
||||
end
|
||||
|
||||
def call_exception_with_status_code(context, status_code)
|
||||
def call_exception_with_status_code(context, exception, status_code)
|
||||
if Kemal.config.error_handlers.has_key?(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)
|
||||
context.response.print Kemal.config.error_handlers[status_code].call(context, exception)
|
||||
context
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@ module Kemal
|
|||
class Config
|
||||
INSTANCE = Config.new
|
||||
HANDLERS = [] of HTTP::Handler
|
||||
ERROR_HANDLERS = {} of Int32 => HTTP::Server::Context -> String
|
||||
ERROR_HANDLERS = {} of Int32 => HTTP::Server::Context, Exception -> String
|
||||
{% if flag?(:without_openssl) %}
|
||||
@ssl : Bool?
|
||||
{% else %}
|
||||
|
@ -72,8 +72,8 @@ module Kemal
|
|||
ERROR_HANDLERS
|
||||
end
|
||||
|
||||
def add_error_handler(status_code, &handler : HTTP::Server::Context -> _)
|
||||
ERROR_HANDLERS[status_code] = ->(context : HTTP::Server::Context) { handler.call(context).to_s }
|
||||
def add_error_handler(status_code, &handler : HTTP::Server::Context, Exception -> _)
|
||||
ERROR_HANDLERS[status_code] = ->(context : HTTP::Server::Context, error : Exception) { handler.call(context, error).to_s }
|
||||
end
|
||||
|
||||
def extra_options(&@extra_options : OptionParser ->)
|
||||
|
|
|
@ -15,6 +15,6 @@ def ws(path, &block : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
|||
Kemal::WebSocketHandler.new path, &block
|
||||
end
|
||||
|
||||
def error(status_code, &block : HTTP::Server::Context -> _)
|
||||
def error(status_code, &block : HTTP::Server::Context, Exception -> _)
|
||||
Kemal.config.add_error_handler status_code, &block
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue