This module provides a function for wrapping a Lambda handler function with error handling and logging.

env_wrapped(func, env_vars)

Wraps the given function with the given environment variables.

Parameters:
  • func (Callable) –

    The function to wrap.

  • env_vars (Mapping[str, str | None]) –

    The environment variables to wrap the function with.

Returns:
  • Callable

    The wrapped function.

Source code in safe_init/handler.py
def env_wrapped(func: Callable, env_vars: Mapping[str, str | None]) -> Callable:
    """
    Wraps the given function with the given environment variables.

    Args:
        func (Callable): The function to wrap.
        env_vars (Mapping[str, str | None]): The environment variables to wrap the function with.

    Returns:
        The wrapped function.
    """

    @wraps(func)
    def _wrapped(*args, **kwargs) -> Any:  # type: ignore[no-untyped-def] # noqa: ANN002, ANN401
        with env(env_vars):
            return func(*args, **kwargs)

    return _wrapped