Skip to content
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 the ability to rescue from specific exception in handlers #283

Open
ellmetha opened this issue Jan 16, 2025 · 0 comments
Open

Add the ability to rescue from specific exception in handlers #283

ellmetha opened this issue Jan 16, 2025 · 0 comments
Assignees
Milestone

Comments

@ellmetha
Copy link
Member

Description

Let's add the ability to specify exceptions to rescue from in handlers by defining a new macro similar to Rails' rescue_from.

Rationale

Presently, the framework does not allow to easily declare exceptions to rescue from in handlers. The only way to do that is to implement some custom rescue logic in the #dispatch or HTTP method-specific method (such as #get or #post for example).

Proposition

Let's add a new #rescue_from macro to handlers that will allow defining specific exceptions that need to be rescued as well as the name of a method that must be executed when these exceptions are indeed rescued:

class ProfileHandler < Marten::Handlers::Template
  include RequireSignedInUser

  rescue_from Auth::UnauthorizedUser, with: :handle_unauthorized_user

  template_name "auth/profile.html"

  private def handle_unauthorized_user
    head :forbidden
  end
end

If the specific callback returns a valid HTTP response, then it should be used as is and returned by the handler. Obviously the callback could also raise a different kind of error, but if the handler returns a value that is not an HTTP response then the original exceptions should be re-raised.

@ellmetha ellmetha added this to the v0.6.0 milestone Jan 16, 2025
@ellmetha ellmetha self-assigned this Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant