Skip to content

Commit 0288f6b

Browse files
authored
Merge pull request #592 from ktetzlaff/improve-error-handling
chore: improve error handling and tests
2 parents b89d990 + a064b98 commit 0288f6b

3 files changed

Lines changed: 177 additions & 32 deletions

File tree

passgithelper.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ def parse_request() -> dict[str, str]:
121121
continue
122122

123123
parts = line.split("=", 1)
124-
assert len(parts) == 2
124+
if len(parts) != 2:
125+
raise ValueError(
126+
f"Missing '=' in request line, cannot be parsed as key/value pair: '{line}'"
127+
)
125128
request[parts[0].strip()] = parts[1].strip()
126129

127130
return request
@@ -494,8 +497,12 @@ def main(argv: Optional[Sequence[str]] = None) -> None:
494497
handle_skip()
495498

496499
action = args.action
500+
if action != "get":
501+
LOGGER.info("Action '%s' is currently not supported", action)
502+
sys.exit(5)
503+
497504
request = parse_request()
498-
LOGGER.debug("Received action %s with request:\n%s", action, request)
505+
LOGGER.debug("Received action '%s' with request:\n%s", action, request)
499506

500507
try:
501508
mapping = parse_mapping(args.mapping)
@@ -504,15 +511,14 @@ def main(argv: Optional[Sequence[str]] = None) -> None:
504511
print(f"Unable to parse mapping file: {error}", file=sys.stderr) # noqa: T201
505512
sys.exit(4)
506513

507-
if action == "get":
508-
try:
509-
get_password(request, mapping)
510-
except Exception as error: # ok'ish for the main function
511-
print(f"Unable to retrieve entry: {error}", file=sys.stderr) # noqa: T201
512-
sys.exit(3) # 1: uncaught exceptions, 2: already used by argparse
513-
else:
514-
LOGGER.info("Action %s is currently not supported", action)
515-
sys.exit(5)
514+
try:
515+
get_password(request, mapping)
516+
except Exception as error: # ok'ish for the main function
517+
print( # noqa: T201
518+
f'Unable to retrieve entry: "{type(error).__name__}: {error}"',
519+
file=sys.stderr,
520+
)
521+
sys.exit(3) # 1: uncaught exceptions, 2: already used by argparse
516522

517523

518524
if __name__ == "__main__":
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# the following line makes this an invalid mapping/ini file
2+
--

0 commit comments

Comments
 (0)