-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace sys.exit calls with exceptions and return values
Nearly all calls to sys.exit are replaced by khard's custom Cancelled exception or an explicit return value of type str|int|None. These are the values that sys.exit can digest and they are handed outwards to the exit call with the return value of main(). This should make the code more testable. Additionally all custom exception are moved to module.
- Loading branch information
Showing
12 changed files
with
134 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import sys | ||
|
||
from .khard import main | ||
|
||
main() | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Custom exceptions for khard""" | ||
|
||
|
||
class Cancelled(Exception): | ||
"""An exception indicating that the user canceled some operation or | ||
some backend operation failed""" | ||
|
||
def __init__(self, message: str = "Canceled", code: int = 1) -> None: | ||
super().__init__(message) | ||
self.code = code | ||
|
||
|
||
class AddressBookParseError(Exception): | ||
"""Indicate an error while parsing data from an address book backend.""" | ||
|
||
def __init__(self, filename: str, abook: str, reason: Exception) -> None: | ||
"""Store the filename that caused the error.""" | ||
super().__init__() | ||
self.filename = filename | ||
self.abook = abook | ||
self.reason = reason | ||
|
||
def __str__(self) -> str: | ||
return f"Error when parsing {self.filename} in address book {self.abook}: {self.reason}" | ||
|
||
|
||
class AddressBookNameError(Exception): | ||
"""Indicate an error with an address book name.""" | ||
|
||
|
||
class ConfigError(Exception): | ||
"""Errors during config file parsing""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.