use dedicated exception for loading errors

fixes #145
This commit is contained in:
lilydjwg 2020-09-11 15:11:21 +08:00
parent f28cc7ae61
commit e744a27572
2 changed files with 19 additions and 4 deletions

View File

@ -33,8 +33,8 @@ 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))
except FileNotFoundError: except core.FileLoadError as e:
sys.exit('version configuration file not given and default does not exist') sys.exit(str(e))
if args.keyfile: if args.keyfile:
keymanager = KeyManager(Path(args.keyfile)) keymanager = KeyManager(Path(args.keyfile))

View File

@ -140,11 +140,23 @@ class Options(NamedTuple):
proxy: Optional[str] proxy: Optional[str]
keymanager: KeyManager keymanager: KeyManager
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]:
config = toml.load(file) try:
config = toml.load(file)
except OSError as e:
raise FileLoadError('version configuration file', e)
ver_files: Optional[Tuple[Path, Path]] = None ver_files: Optional[Tuple[Path, Path]] = None
keymanager = KeyManager(None) keymanager = KeyManager(None)
@ -167,7 +179,10 @@ 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
keymanager = KeyManager(keyfile) try:
keymanager = KeyManager(keyfile)
except OSError as e:
raise FileLoadError('keyfile', e)
max_concurrency = c.get('max_concurrency', 20) max_concurrency = c.get('max_concurrency', 20)
proxy = c.get('proxy') proxy = c.get('proxy')