-
-
Notifications
You must be signed in to change notification settings - Fork 188
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -11,12 +11,44 @@ module Kemal | |||||
rescue ex : Kemal::Exceptions::CustomException | ||||||
call_exception_with_status_code(context, ex, context.response.status_code) | ||||||
rescue ex : Exception | ||||||
# Use error handler defined for the current exception if it exists | ||||||
return call_exception_with_exception(context, ex, 500) if Kemal.config.error_handlers.has_key?(ex.class) | ||||||
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. This line is added above the A logger is needed for This shouldn't be a problem in the real world case as a logger would always exist afaik. Please let me know if this should be rectified in some other way. 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. This approach won't work with inheritance. 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. What do you mean by inheritance in this case? 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. Calling 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 suppose it should be something like 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. That does identify whether the exception inherits from a known exception but shouldn't this also take into account the order of inheritance? For example class GrandParentException < Exception
end
class ParentException < GrandParentException
end
class ChildException < ParentException
end
error Exception do
"Generic"
end
error GrandParentException do
"Grandparent exception"
end
error ParentException do
"Parent exception"
end
get "/" do
raise ChildException.new()
end I'll expect that the raised error in the above logic be caught by the handler for Is there any way of identify the order of inheritance at runtime? 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. Sure, you could sort classes by their inheritance relationship. I don't think that would be a good idea though. Just following the order of declaration is probably best. 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. True. In that case I think this should achieve the desired behavior
Comment on lines
+14
to
+15
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. suggestion: This special case with
Suggested change
|
||||||
|
||||||
# Use error handler for an ancestor of the current exception if it exists | ||||||
Kemal.config.error_handlers.each_key do |key| | ||||||
if key.is_a? Exception.class && ex.class <= key | ||||||
return call_exception_with_exception(context, ex, 500, override_handler_used: key) | ||||||
end | ||||||
end | ||||||
|
||||||
log("Exception: #{ex.inspect_with_backtrace}") | ||||||
# Else use generic 500 handler if defined | ||||||
return call_exception_with_status_code(context, ex, 500) if Kemal.config.error_handlers.has_key?(500) | ||||||
verbosity = Kemal.config.env == "production" ? false : true | ||||||
render_500(context, ex, verbosity) | ||||||
end | ||||||
|
||||||
# Calls the defined error handler for the given exception if it exists | ||||||
# | ||||||
# By default it tries to use the handler that is defined for the given exception. However, another | ||||||
# handler can be used via the `override_handler_used` parameter. | ||||||
private def call_exception_with_exception(context : HTTP::Server::Context, exception : Exception, status_code : Int32 = 500, override_handler_used : (Exception.class)? = nil) | ||||||
return if context.response.closed? | ||||||
|
||||||
if !override_handler_used | ||||||
handler_to_use = exception.class | ||||||
else | ||||||
handler_to_use = override_handler_used | ||||||
end | ||||||
|
||||||
if !Kemal.config.error_handlers.empty? && Kemal.config.error_handlers.has_key?(handler_to_use) | ||||||
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. performance: The combination of |
||||||
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type") | ||||||
context.response.status_code = status_code | ||||||
context.response.print Kemal.config.error_handlers[handler_to_use].call(context, exception) | ||||||
context | ||||||
end | ||||||
end | ||||||
|
||||||
private def call_exception_with_status_code(context : HTTP::Server::Context, exception : Exception, status_code : Int32) | ||||||
return if context.response.closed? | ||||||
if !Kemal.config.error_handlers.empty? && Kemal.config.error_handlers.has_key?(status_code) | ||||||
|
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.
thought: I'm wondering if it makes sense to mash up
Int32
andException.class
keys in a single hash.Might be better to have two separate collections. They're used completely independent of each other. There's no need to expose both under the same name
Kemal.error_handlers
.Maybe we could introduce a new name
Kemal.exception_handlers
for this feature?