Closed
Description
Use case
I find myself often writing a generic try/except statement in my handler to always log all exceptions. I'm not sure if this is a good pattern, maybe it already does this, so I might just be looking for guidance on current practices. (I didn't see anything in the docs or other issues on this.)
from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger()
@logger.inject_lambda_context
def handler(event: dict, context: LambdaContext) -> str:
try:
return "hello world"
except Exception:
logger.exception("Caught an unhandled error")
raise
Solution/User Experience
What I've done in CLI utilities to capture those exceptions to the logger, is to override the sys.eventhook
. This way, every exception is logged automatically. Would it be useful to do something similar for lambda functions?
def exception_hook(exc_type, exc_value, exc_traceback):
"""Log unhandled exceptions with hook for sys.excepthook."""
log = logging.getLogger(service_name)
log.exception(
'',
exc_info=(exc_type, exc_value, exc_traceback)
)
sys.excepthook = exception_hook
Alternative solutions
No response
Acknowledgment
- This feature request meets Lambda Powertools Tenets
- Should this be considered in other Lambda Powertools languages? i.e. Java, TypeScript