Error handling with exception mappersΒΆ

For basic error handling, Jivago provides an ExceptionMapper mechanism, which can automatically create an HTTP response for an uncaught exception. To define a custom exception mapper, create a component which inherits from the ExceptionMapper interface, implementing the handles and create_response methods.

from jivago.inject.annotation import Component
from jivago.lang.annotations import Override
from jivago.wsgi.filter.system_filters.error_handling.exception_mapper import ExceptionMapper
from jivago.wsgi.request.response import Response

class TeapotExceptionMapper(ExceptionMapper):

    def handles(self, exception: Exception) -> bool:
        return exception == TeapotException

    def create_response(self, exception: Exception) -> Response:
        return Response(418, {}, "Error! I am a teapot!")
  • handles(exception) -> bool : Used to find the corresponding exception mapper.
  • create_response(exception) -> Response : Creates the actual HTTP response.