Skip to content

module Athena::Framework::Exception #

Exception handling in Athena is similar to exception handling in any Crystal program, with the addition of a new unique exception type, ATH::Exception::HTTPException.

When an exception is raised, Athena emits the ATH::Events::Exception event to allow an opportunity for it to be handled. If the exception goes unhandled, i.e. no listener set an ATH::Response on the event, then the request is finished and the exception is reraised. Otherwise, that response is returned, setting the status and merging the headers on the exceptions if it is an ATH::Exception::HTTPException. See ATH::Listeners::Error and ATH::ErrorRendererInterface for more information on how exceptions are handled by default.

To provide the best response to the client, non ATH::Exception::HTTPException should be rescued and converted into a corresponding ATH::Exception::HTTPException. Custom HTTP errors can also be defined by inheriting from ATH::Exception::HTTPException or a child type. A use case for this could be allowing for additional data/context to be included within the exception that ultimately could be used in a ATH::Events::Exception listener.