Rename parsing error to be more reusable

This commit is contained in:
Campbell Alden 2026-08-02 22:49:38 +09:00
parent 0af1b827aa
commit 328f430309
2 changed files with 7 additions and 7 deletions

View file

@ -2,7 +2,7 @@ from typing import Any
from dataclasses import dataclass from dataclasses import dataclass
import logging import logging
from .parse import assert_key_of_type, ConfigParseError from .parse import assert_key_of_type, ParseError
LOG_LEVELS = { LOG_LEVELS = {
'critical': logging.CRITICAL, 'critical': logging.CRITICAL,
@ -25,7 +25,7 @@ class Logging:
assert_key_of_type(config, 'level', str) assert_key_of_type(config, 'level', str)
log_level_setting = config['level'] log_level_setting = config['level']
if log_level_setting not in LOG_LEVELS: if log_level_setting not in LOG_LEVELS:
raise ConfigParseError( raise ParseError(
['level'], f'unknown log level "{log_level_setting}". Expected one of: {", ".join(LOG_LEVELS.keys())}' ['level'], f'unknown log level "{log_level_setting}". Expected one of: {", ".join(LOG_LEVELS.keys())}'
) )

View file

@ -1,7 +1,7 @@
from typing import Any, TypeVar, Callable from typing import Any, TypeVar, Callable
class ConfigParseError(Exception): class ParseError(Exception):
"""An error for when parsing a config fails""" """An error for when parsing a config fails"""
def __init__(self, keypath: list[str], issue: str): def __init__(self, keypath: list[str], issue: str):
@ -14,10 +14,10 @@ class ConfigParseError(Exception):
def assert_key_of_type(config: dict[str, Any], key: str, kind: Any): def assert_key_of_type(config: dict[str, Any], key: str, kind: Any):
if key not in config: if key not in config:
raise ConfigParseError([key], 'missing') raise ParseError([key], 'missing')
if not isinstance(config[key], kind): if not isinstance(config[key], kind):
raise ConfigParseError([key], f'type of "{key}" was {type(config[key])}, expected {kind}') raise ParseError([key], f'type of "{key}" was {type(config[key])}, expected {kind}')
T = TypeVar('T') T = TypeVar('T')
@ -27,5 +27,5 @@ def parse_nested_config(config: dict[str, Any], key: str, parse: Callable[[dict[
assert_key_of_type(config, key, dict) assert_key_of_type(config, key, dict)
try: try:
return parse(config[key]) return parse(config[key])
except ConfigParseError as e: except ParseError as e:
raise ConfigParseError([key, *e.keypath], e.issue) raise ParseError([key, *e.keypath], e.issue)