Skip to content

Add ability to add handlers for raised exceptions #688

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

Merged
merged 9 commits into from
Dec 19, 2024

Conversation

syeopite
Copy link
Contributor

Closes #622

Description of the Change

This PR adds the ability for users to define error handlers that are used when a specific exception is raised.

require "kemal"

class NewException < Exception
end

get "/" do | env |
  raise NewException.new()
end

error NewException do | env |
  "An error occured!"
end

Kemal.run

Alternate Designs

Benefits

This provides for a cleaner and easier experience for defining custom errors. See #622.

Possible Drawbacks

@@ -11,12 +11,26 @@ 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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is added above the log function in order for the specs to pass.

A logger is needed for #log to be able to successfully run which the tests in exception_handler_spec.cr doesn't appear to have defined.

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.

@robacarp
Copy link

This is perfect!

Co-authored-by: Johannes Müller <straightshoota@gmail.com>
@syeopite syeopite force-pushed the handle-custom-errors branch from e7f441a to bf19e27 Compare September 20, 2024 02:36
`ex.class <= key` below should already handle the exact match

```crystal
Kemal.config.error_handlers.each_key do |key|
  if ex.class <= key
  end
end

```
Prior to this commit, handlers for exceptions and handlers for
HTTP status codes were stored in a single collection. However,
the two are used completely independently from each other and
should instead be stored in two separate collections.
Copy link
Member

@sdogruyol sdogruyol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @syeopite, thanks a lot 👍 Waiting for the review of @straight-shoota

Co-authored-by: Johannes Müller <straightshoota@gmail.com>
Copy link
Member

@sdogruyol sdogruyol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @syeopite 👍

@sdogruyol sdogruyol merged commit 6b884dd into kemalcr:master Dec 19, 2024
6 of 9 checks passed
@syeopite syeopite deleted the handle-custom-errors branch December 19, 2024 13:15
syeopite added a commit to syeopite/invidious that referenced this pull request Dec 19, 2024
Kilt is unmaintained and the ECR templating logic has been
natively integrated into Kemal with the issues previously seen
having been resolved.

This commit is mostly a precursor to support the next Kemal
release which will add the ability to create error handlers for
raised exceptions.

See kemalcr/kemal#688
syeopite added a commit to syeopite/invidious that referenced this pull request Dec 19, 2024
Kilt is unmaintained and the ECR templating logic has been
natively integrated into Kemal with the issues previously seen
having been resolved.

This commit is mostly a precursor to support the next Kemal
release which will add the ability to create error handlers for
raised exceptions.

See kemalcr/kemal#688
syeopite added a commit to syeopite/invidious that referenced this pull request Jan 29, 2025
Kilt is unmaintained and the ECR templating logic has been
natively integrated into Kemal with the issues previously seen
having been resolved.

This commit is mostly a precursor to support the next Kemal
release which will add the ability to create error handlers for
raised exceptions.

See kemalcr/kemal#688
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow handling errors with error handlers
5 participants