Skip to content

Commit

Permalink
fix: Handle ConfigurationError correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed Jun 24, 2024
1 parent c0300df commit 71ccc1d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
9 changes: 8 additions & 1 deletion src/ai/backend/web/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,21 @@ def status(
local_config = load_local_config(config_path, log_level, debug=debug)
except ConfigurationError as e:
print(
"ConfigurationError: Could not read or validate the webserver local config:",
"ConfigurationError: Could not read or validate the webserver local config.",
file=sys.stderr,
)
print(pformat(e.invalid_data), file=sys.stderr)
raise click.Abort()

pid_filepath = local_config["webserver"]["pid-file"]

if not pid_filepath.is_file():
print(
'ConfigurationError: "pid-file" not found in the configuration file.',
file=sys.stderr,
)
raise click.Abort()

with open(pid_filepath, "r") as file:
agent_pid = int(file.read())

Expand Down
28 changes: 13 additions & 15 deletions src/ai/backend/web/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
import sys
from pathlib import Path
from pprint import pformat
from typing import Any, Mapping

import pkg_resources
Expand Down Expand Up @@ -163,23 +161,23 @@
def load_local_config(
config_path: Path, log_level: LogSeverity, debug: bool = False
) -> dict[str, Any]:
# Delete this part when you remove --debug option
raw_cfg = tomli.loads(Path(config_path).read_text(encoding="utf-8"))
try:
# Delete this part when you remove --debug option
raw_cfg = tomli.loads(Path(config_path).read_text(encoding="utf-8"))
except IOError:
raise ConfigurationError({
"load_local_config()": (
f"Could not read the webserver local config file: {config_path}"
)
})

if debug:
log_level = LogSeverity.DEBUG

config.override_key(raw_cfg, ("debug", "enabled"), log_level == LogSeverity.DEBUG)
config.override_key(raw_cfg, ("logging", "level"), log_level)
config.override_key(raw_cfg, ("logging", "pkg-ns", "ai.backend"), log_level)

try:
cfg = config.check(raw_cfg, webserver_local_config_iv)
config.set_if_not_set(cfg, ("pipeline", "frontend-endpoint"), cfg["pipeline"]["endpoint"])
return cfg
except ConfigurationError as e:
print(
"ConfigurationError: Validation of webserver config has failed:",
file=sys.stderr,
)
print(pformat(e.invalid_data), file=sys.stderr)
raise
cfg = config.check(raw_cfg, webserver_local_config_iv)
config.set_if_not_set(cfg, ("pipeline", "frontend-endpoint"), cfg["pipeline"]["endpoint"])
return cfg

0 comments on commit 71ccc1d

Please sign in to comment.