If importing the main module fails, this takes the original exception, converts to to a string, and then appends to the ImportError, which masks details about the original exception, including its actual stack trace, and the type of exception.
As an example, suppose my main module does os.environ["ABC"], where there is no ABC environment variable. With the current code, I get:
Runtime.ImportModuleError: Unable to import module 'newrelic_lambda_wrapper': Failed to import module '\<name>': 'ABC'
That does not make it clear at all what the problem is, or even that it was a KeyError.
Ideally, this wrapper would instead do something like:
raise ImportError("Failed to import module '%s'" % module_path) from e
https://github.com/newrelic/newrelic-lambda-layers/blob/48654ab84f3c2fb779a6c33dae769b3083a76214/python/newrelic_lambda_wrapper.py#L65
If importing the main module fails, this takes the original exception, converts to to a string, and then appends to the
ImportError
, which masks details about the original exception, including its actual stack trace, and the type of exception.As an example, suppose my main module does
os.environ["ABC"]
, where there is noABC
environment variable. With the current code, I get:That does not make it clear at all what the problem is, or even that it was a
KeyError
.Ideally, this wrapper would instead do something like:
Unfortunately, it sounds like the Lambda runtime neglects to output the exception chain properly. https://stackoverflow.com/questions/62112585/aws-lambda-not-showing-cause-exception-stacktrace-in-python-3-8
So this wrapper may instead need to collect the traceback (and exception type) itself, and glue all of that into the
ImportModuleError
.