aws_lambda_decorators/decorators.py
def handle_exceptions(handlers):
    """
    Handles exceptions thrown by the wrapped/decorated function.

    Usage:
        @handle_exceptions([ExceptionHandler(exception=KeyError, friendly_message="Your message on KeyError except")]).
        def lambda_handler(params)
            pass

    Args:
        handlers (list): A collection of ExceptionHandler type items.
    """
    def decorator(func):
        def wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except tuple(handler.exception for handler in handlers) as ex:  # noqa: pylint - catching-non-exception
                failed_handler = [handler for handler in handlers if isinstance(ex, handler.exception)][0]
                message = failed_handler.friendly_message

                if message and str(ex):
                    LOGGER.error("%s: %s", message, str(ex))
                else:
                    LOGGER.error(message if message else str(ex))

                return failure(message if message else str(ex), failed_handler.status_code)
        return wrapper
    return decorator

    Handles exceptions thrown by the wrapped/decorated function.

    Usage:
        @handle_exceptions([ExceptionHandler(exception=KeyError, friendly_message="Your message on KeyError except")]).
        def lambda_handler(params)
            pass

    Args:
        handlers (list): A collection of ExceptionHandler type items.
    
Handles exceptions thrown by the wrapped/decorated function.
Handles exceptions thrown by the wrapped / decorated function .
def handle_exceptions(handlers):
    def decorator(func):
        def wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except tuple(handler.exception for handler in handlers) as ex:  
                failed_handler = [handler for handler in handlers if isinstance(ex, handler.exception)][0]
                message = failed_handler.friendly_message
                if message and str(ex):
                    LOGGER.error("%s: %s", message, str(ex))
                else:
                    LOGGER.error(message if message else str(ex))
                return failure(message if message else str(ex), failed_handler.status_code)
        return wrapper
    return decorator
def handle_exceptions ( handlers ) : def decorator ( func ) : def wrapper ( * args , ** kwargs ) : try : return func ( * args , ** kwargs ) except tuple ( handler . exception for handler in handlers ) as ex : failed_handler = [ handler for handler in handlers if isinstance ( ex , handler . exception ) ] [ 0 ] message = failed_handler . friendly_message if message and str ( ex ) : LOGGER . error ( "%s: %s" , message , str ( ex ) ) else : LOGGER . error ( message if message else str ( ex ) ) return failure ( message if message else str ( ex ) , failed_handler . status_code ) return wrapper return decorator
Handles exceptions thrown by the wrapped/decorated function.
Handles exceptions thrown by the wrapped / decorated function .
"""
    Handles exceptions thrown by the wrapped/decorated function.

    Usage:
        @handle_exceptions([ExceptionHandler(exception=KeyError, friendly_message="Your message on KeyError except")]).
        def lambda_handler(params)
            pass

    Args:
        handlers (list): A collection of ExceptionHandler type items.
    """ # noqa: pylint - catching-non-exception
handlers
A collection of ExceptionHandler type items.
A collection of ExceptionHandler type items .