Repository navigation
fix(cli): name the --app target and exception type when the import fails - #864
Merged
Merged
Conversation
Importing the --app module runs module-level user code, which can raise anything. Only ImportError, ValueError and AttributeError were reported with the target; any other exception fell through to a generic handler that printed just the exception text, for example a bare KeyError message for a missing environment variable. Report every exception raised by the import as: Error: Could not import module from --app <value>: <ExceptionType>: <message>. The exit code and the empty stdout are unchanged. Refs #860 Co-authored-by: sisyphus-dev-ai <sisyphus-dev-ai@users.noreply.github.com>
The --app warning recommends guarding external initialization with an environment flag but showed no example. Add a minimal guard, the CI invocation, and the error message a still-failing import produces. Refs #860 Co-authored-by: sisyphus-dev-ai <sisyphus-dev-ai@users.noreply.github.com>
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the CLI import-failure diagnostics change.
No open issues 🎉
What changed in this PR
Improves CLI diagnostics for --app import failures, addressing #860 by identifying the target and exception type while preserving exit code 1 and empty stdout.
Changes:
- Broadens import-error handling to catch
Exceptionand report contextual errors. - Adds regression tests for import-time
KeyErrorand existing failure cases. - Documents an environment-flag guard for external initialization during spec generation.
| File | Description |
|---|---|
tests/test_cli.py |
Tests error context, exception types, exit codes, and empty stdout. |
src/azure_functions_openapi/cli.py |
Reports the --app target and exception type for import failures. |
docs/cli.md |
Adds a guarded initialization example, CI command, and failure message. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
azure-functions-openapi generate --app module:variableimports the module, so an exception raised by module-level code surfaced as one bare line, for exampleFailed to generate OpenAPI specification: 'SECRET_KEY', with no mention of the--apptarget. Every exception raised while importing the target is now reported with the target and the exception type.Changes
handle_generatereports any exception raised by the--appimport asError: Could not import module from --app '<value>': <ExceptionType>: <message>. OnlyImportError,ValueErrorandAttributeErrorwere reported this way before. The exit code (1) and the empty stdout are unchanged.docs/cli.mdshows a minimal environment-flag guard for import-time side effects next to the existing warning, the CI invocation, and the message a still-failing import produces.Validation
Run locally:
make check-all: exit 0 (1664 passed, 30 skipped). The skipped tests need the optionalazure-functions-validationpackage, which the Hatch environment does not install.env -u SECRET_KEY azure-functions-openapi generate --app my_app:app) printsError: Could not import module from --app 'my_app:app': KeyError: 'SECRET_KEY'with exit code 1 and empty stdout. Onorigin/mainit prints onlyFailed to generate OpenAPI specification: 'SECRET_KEY'.GitHub CI on
9e5e3cd: all check runs completed with no failures, including the requiredAnalyze,bandit,ci-required,PR titleandsemgrepcontexts.Compatibility
The text of the error changes for the three exception types that were already reported: it now includes the exception type, for example
ModuleNotFoundError: No module named 'x'. No exit code changes.Related Issues
Closes #860