module Marten::Handlers::ExceptionHandling

Overview

Provides the ability to define exception handlers.

This module provides the ability to define exception handlers that are executed when an exception is raised during the execution of the handler's #dispatch method. These handlers can be defined by using the #rescue_from macro, which accepts one or more exception classes and an exception handler that can be specified by a trailing :with option containing the name of a method to invoke or a block containing the exception handling logic.

For example:

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

  template_name "auth/profile.html"

  rescue_from Auth::UnauthorizedUser, with: :handle_unauthorized_user

  rescue_from OtherError do
    redirect reverse("home")
  end

  private def handle_unauthorized_user
    head :forbidden
  end
end

Exception handlers can be inherited from parent classes. They are searched bottom-up in the inheritance hierarchy.

Direct including types

Defined in:

marten/handlers/concerns/exception_handling.cr

Macro Summary

Macro Detail

macro rescue_from(*exception_klasses, **kwargs, &block) #

Allows to define an exception handler for a specific exception.

The rescue_from macro accepts one or more exception classes and an exception handler that can be specified by a trailing :with option containing the name of a method to invoke or a block containing the exception handling logic.

For example:

rescue_from Auth::UnauthorizedUser, with: :handle_unauthorized_user

rescue_from OtherError do
  redirect reverse("home")
end

[View source]