Skip to content

Commit

Permalink
Improved exception handling in Keyring methods
Browse files Browse the repository at this point in the history
Details:

* Added missing exception handling to 'Keyring.set_password()' and improved
  exception chaining in 'Keyring.get_password()'.

Signed-off-by: Andreas Maier <andreas.r.maier@gmx.de>
  • Loading branch information
andy-maier committed Apr 4, 2021
1 parent 2a61ff3 commit d99060a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Released: not yet
* Fixed that the package did not contain the files for the 'easy-vault'
command. (issue #45)

* Added missing exception handling to 'Keyring.set_password()' and improved
exception chaining in 'Keyring.get_password()'.

**Enhancements:**

**Cleanup:**
Expand Down
22 changes: 18 additions & 4 deletions easy_vault/_keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ def get_password(self, filepath):
return keyring.get_password(
self.keyring_service(), self.keyring_username(filepath))
except NO_KEYRING_EXCEPTION as exc:
raise KeyringNotAvailable(str(exc))
new_exc = KeyringNotAvailable(str(exc))
new_exc.__cause__ = None
raise new_exc # KeyringNotAvailable
except keyring.errors.KeyringError as exc:
raise KeyringError(str(exc))
new_exc = KeyringError(str(exc))
new_exc.__cause__ = None
raise new_exc # KeyringError

def set_password(self, filepath, password):
"""
Expand All @@ -125,8 +129,18 @@ def set_password(self, filepath, password):
:exc:`KeyringNotAvailable`: No keyring service available.
:exc:`KeyringError`: An error happend in the keyring service.
"""
keyring.set_password(
self.keyring_service(), self.keyring_username(filepath), password)
try:
keyring.set_password(
self.keyring_service(),
self.keyring_username(filepath), password)
except NO_KEYRING_EXCEPTION as exc:
new_exc = KeyringNotAvailable(str(exc))
new_exc.__cause__ = None
raise new_exc # KeyringNotAvailable
except keyring.errors.KeyringError as exc:
new_exc = KeyringError(str(exc))
new_exc.__cause__ = None
raise new_exc # KeyringError

def is_available(self):
"""
Expand Down

0 comments on commit d99060a

Please sign in to comment.