newrelic / newrelic-lambda-layers

Source code and utilities to build and publish New Relic's public AWS Lambda layers.
https://newrelic.com/products/serverless-aws-lambda
Apache License 2.0
36 stars 43 forks source link

ImportError masks original exception #281

Open rittneje opened 1 week ago

rittneje commented 1 week ago

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 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

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.

workato-integration[bot] commented 1 week ago

https://new-relic.atlassian.net/browse/NR-336930