-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch unexpected errors reading config file
When reading the `__appsignal__.py` configuration file from the demo command, handle unexpected errors in the file (e.g. syntax errors) instead of only handling the expected error of the `appsignal` variable being missing. Unify the handling of errors when reading a config file. Provide an `ExitError` that can be raised by command helpers to trigger the command to exit. Since loading the configuration file is only done by the diagnose and demo commands, move that functionality out of the `Client` file and into a helper for the CLI commands.
- Loading branch information
Showing
10 changed files
with
77 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
from runpy import run_path | ||
|
||
from ..client import Client | ||
|
||
|
||
# Load the AppSignal client from the app specific `__appsignal__.py` client | ||
# file. This loads the user config, rather than our default config. | ||
# If no client file is found it return `None`. | ||
# If there's a problem with the client file it will raise an | ||
# `InvalidClientFileError` with a message containing more details. | ||
def _client_from_config_file() -> Client | None: | ||
cwd = os.getcwd() | ||
app_config_path = os.path.join(cwd, "__appsignal__.py") | ||
if os.path.exists(app_config_path): | ||
try: | ||
client = run_path(app_config_path)["appsignal"] | ||
if not isinstance(client, Client): | ||
raise InvalidClientFileError( | ||
"The `appsignal` variable does not contain an AppSignal client. " | ||
"Please define the configuration file as described in " | ||
"our documentation: " | ||
"https://docs.appsignal.com/python/configuration.html" | ||
) | ||
|
||
return client | ||
except KeyError as error: | ||
raise InvalidClientFileError( | ||
"No `appsignal` variable found. " | ||
"Please define the configuration file as described in " | ||
"our documentation: " | ||
"https://docs.appsignal.com/python/configuration.html" | ||
) from error | ||
|
||
return None | ||
|
||
|
||
class InvalidClientFileError(Exception): | ||
pass |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from __future__ import annotations | ||
|
||
|
||
class ExitError(Exception): | ||
exit_code: int | ||
|
||
def __init__(self, exit_code: int) -> None: | ||
self.exit_code = exit_code |
This file contains 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
This file contains 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
This file contains 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
Submodule diagnose
updated
4 files
+1 −1 | Gemfile.lock | |
+2 −1 | bin/test-all | |
+12 −3 | spec/diagnose_spec.rb | |
+2 −1 | spec/support/runner.rb |