give better error messages when failing to load file

This commit is contained in:
lilydjwg 2020-10-24 16:31:18 +08:00
parent a8073e2ad5
commit b83f92cc67
3 changed files with 23 additions and 21 deletions

View File

@ -33,14 +33,14 @@ def main() -> None:
try: try:
entries, options = core.load_file( entries, options = core.load_file(
args.file, use_keymanager=not bool(args.keyfile)) args.file, use_keymanager=not bool(args.keyfile))
if args.keyfile:
keymanager = KeyManager(Path(args.keyfile))
else:
keymanager = options.keymanager
except core.FileLoadError as e: except core.FileLoadError as e:
sys.exit(str(e)) sys.exit(str(e))
if args.keyfile:
keymanager = KeyManager(Path(args.keyfile))
else:
keymanager = options.keymanager
if options.proxy is not None: if options.proxy is not None:
ctx_proxy.set(options.proxy) ctx_proxy.set(options.proxy)

View File

@ -29,6 +29,7 @@ from . import slogconf
from .util import ( from .util import (
Entry, Entries, KeyManager, RawResult, Result, VersData, Entry, Entries, KeyManager, RawResult, Result, VersData,
FunctionWorker, GetVersionError, FunctionWorker, GetVersionError,
FileLoadError,
) )
from . import __version__ from . import __version__
from .sortversion import sort_version_keys from .sortversion import sort_version_keys
@ -149,22 +150,14 @@ class Options(NamedTuple):
httplib: Optional[str] httplib: Optional[str]
http_timeout: int http_timeout: int
class FileLoadError(Exception):
def __init__(self, kind, exc):
self.kind = kind
self.exc = exc
def __str__(self):
return f'failed to load {self.kind}: {self.exc}'
def load_file( def load_file(
file: str, *, file: str, *,
use_keymanager: bool, use_keymanager: bool,
) -> Tuple[Entries, Options]: ) -> Tuple[Entries, Options]:
try: try:
config = toml.load(file) config = toml.load(file)
except OSError as e: except (OSError, toml.TomlDecodeError) as e:
raise FileLoadError('version configuration file', e) raise FileLoadError('version configuration file', file, e)
ver_files: Optional[Tuple[Path, Path]] = None ver_files: Optional[Tuple[Path, Path]] = None
keymanager = KeyManager(None) keymanager = KeyManager(None)
@ -189,10 +182,7 @@ def load_file(
keyfile_s = os.path.expandvars( keyfile_s = os.path.expandvars(
os.path.expanduser(c.get('keyfile'))) os.path.expanduser(c.get('keyfile')))
keyfile = d / keyfile_s keyfile = d / keyfile_s
try: keymanager = KeyManager(keyfile)
keymanager = KeyManager(keyfile)
except OSError as e:
raise FileLoadError('keyfile', e)
if 'source' in c: if 'source' in c:
source_configs = c['source'] source_configs = c['source']

View File

@ -37,14 +37,26 @@ VersionResult.__doc__ = '''The result of a `get_version` check.
* `Exception` - An error occurred. * `Exception` - An error occurred.
''' '''
class FileLoadError(Exception):
def __init__(self, kind, filename, exc):
self.kind = kind
self.filename = filename
self.exc = exc
def __str__(self):
return f'failed to load {self.kind} {self.filename!r}: {self.exc}'
class KeyManager: class KeyManager:
'''Manages data in the keyfile.''' '''Manages data in the keyfile.'''
def __init__( def __init__(
self, file: Optional[Path], self, file: Optional[Path],
) -> None: ) -> None:
if file is not None: if file is not None:
with file.open() as f: try:
keys = toml.load(f)['keys'] with file.open() as f:
keys = toml.load(f)['keys']
except (OSError, toml.TomlDecodeError) as e:
raise FileLoadError('keyfile', str(file), e)
else: else:
keys = {} keys = {}
self.keys = keys self.keys = keys