Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError masks original exception #281

Open
rittneje opened this issue Nov 5, 2024 · 6 comments
Open

ImportError masks original exception #281

rittneje opened this issue Nov 5, 2024 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@rittneje
Copy link

rittneje commented Nov 5, 2024

raise ImportError("Failed to import module '%s': %s" % (module_path, e))

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.

@rittneje rittneje added the bug Something isn't working label Nov 5, 2024
@workato-integration
Copy link

@aavinash-nr
Copy link
Contributor

Hello @rittneje ,
We have checked all the error messages which are being shown from NR Extension and we found that we are capturing the trace for all the errors.

[ERROR] Runtime.ImportModuleError: Unable to import module 'newrelic_lambda_wrapper': Failed to import module 'app': No module named 'ABC'

The above error message correctly indicates that module named ABC could not be imported. Let us know if any other issue is present.

Thank you!

@rittneje
Copy link
Author

@aavinash-nr That is not a trace. As you can see, it did not output the original exception type or traceback. As I mentioned in the issue description, it only outputs the text of the original exception, which is not helpful in general. Please re-open this issue.

@rittneje
Copy link
Author

@aavinash-nr Following up on this. Please re-open this issue.

@chaudharysaket
Copy link
Contributor

Hi @rittneje
I have re-opened the issue.
While testing, we did not encounter the issue of the ImportModuleError not being propagated.
Could you please share some examples.
Thank you!

@rittneje
Copy link
Author

rittneje commented Feb 12, 2025

@chaudharysaket Please re-read the issue description. The problem is not ImportModuleError itself, but rather that if my module raises an exception while initializing global variables (say), the original exception type and stack trace get lost.

For example, if my main module does:

import os

ABC = os.environ["ABC"]

and there is no ABC environment variable, that throws a KeyError, but the only thing that gets logged in the Lambda logs is

Runtime.ImportModuleError: Unable to import module 'newrelic_lambda_wrapper': Failed to import module '<name>': 'ABC'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants