Rename parsing error to be more reusable
This commit is contained in:
parent
0af1b827aa
commit
328f430309
2 changed files with 7 additions and 7 deletions
|
|
@ -2,7 +2,7 @@ from typing import Any
|
|||
from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from .parse import assert_key_of_type, ConfigParseError
|
||||
from .parse import assert_key_of_type, ParseError
|
||||
|
||||
LOG_LEVELS = {
|
||||
'critical': logging.CRITICAL,
|
||||
|
|
@ -25,7 +25,7 @@ class Logging:
|
|||
assert_key_of_type(config, 'level', str)
|
||||
log_level_setting = config['level']
|
||||
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())}'
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from typing import Any, TypeVar, Callable
|
||||
|
||||
|
||||
class ConfigParseError(Exception):
|
||||
class ParseError(Exception):
|
||||
"""An error for when parsing a config fails"""
|
||||
|
||||
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):
|
||||
if key not in config:
|
||||
raise ConfigParseError([key], 'missing')
|
||||
raise ParseError([key], 'missing')
|
||||
|
||||
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')
|
||||
|
|
@ -27,5 +27,5 @@ def parse_nested_config(config: dict[str, Any], key: str, parse: Callable[[dict[
|
|||
assert_key_of_type(config, key, dict)
|
||||
try:
|
||||
return parse(config[key])
|
||||
except ConfigParseError as e:
|
||||
raise ConfigParseError([key, *e.keypath], e.issue)
|
||||
except ParseError as e:
|
||||
raise ParseError([key, *e.keypath], e.issue)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue