-
-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to add handlers for raised exceptions #688
base: master
Are you sure you want to change the base?
Changes from all commits
8c4fca3
9c64861
bf19e27
f51fda8
122d77e
6b12977
cd72a7e
ac63711
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,11 +8,12 @@ module Kemal | |||||
# Kemal.config | ||||||
# ``` | ||||||
class Config | ||||||
INSTANCE = Config.new | ||||||
HANDLERS = [] of HTTP::Handler | ||||||
CUSTOM_HANDLERS = [] of Tuple(Nil | Int32, HTTP::Handler) | ||||||
FILTER_HANDLERS = [] of HTTP::Handler | ||||||
ERROR_HANDLERS = {} of Int32 => HTTP::Server::Context, Exception -> String | ||||||
INSTANCE = Config.new | ||||||
HANDLERS = [] of HTTP::Handler | ||||||
CUSTOM_HANDLERS = [] of Tuple(Nil | Int32, HTTP::Handler) | ||||||
FILTER_HANDLERS = [] of HTTP::Handler | ||||||
ERROR_HANDLERS = {} of Int32 => HTTP::Server::Context, Exception -> String | ||||||
EXCEPTION_HANDLERS = {} of Exception.class => HTTP::Server::Context, Exception -> String | ||||||
|
||||||
{% if flag?(:without_openssl) %} | ||||||
@ssl : Bool? | ||||||
|
@@ -88,14 +89,26 @@ module Kemal | |||||
FILTER_HANDLERS << handler | ||||||
end | ||||||
|
||||||
# Returns the defined error handlers for HTTP status codes | ||||||
def error_handlers | ||||||
ERROR_HANDLERS | ||||||
end | ||||||
|
||||||
# Adds an error handler for the given HTTP status code | ||||||
def add_error_handler(status_code : Int32, &handler : HTTP::Server::Context, Exception -> _) | ||||||
ERROR_HANDLERS[status_code] = ->(context : HTTP::Server::Context, error : Exception) { handler.call(context, error).to_s } | ||||||
end | ||||||
|
||||||
# Returns tje defined error handlers for exceptions | ||||||
def exception_handlers | ||||||
EXCEPTION_HANDLERS | ||||||
end | ||||||
|
||||||
# Adds an error handler for the given exception | ||||||
def add_exception_handler(exception : Exception.class, &handler : HTTP::Server::Context, Exception -> _) | ||||||
EXCEPTION_HANDLERS[exception] = ->(context : HTTP::Server::Context, error : Exception) { handler.call(context, error).to_s } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this can be added as given, avoiding an additional closure.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I agree requiring |
||||||
end | ||||||
|
||||||
def extra_options(&@extra_options : OptionParser ->) | ||||||
end | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: