diff --git a/changelog.md b/changelog.md index cb39ab34..6c8547d0 100644 --- a/changelog.md +++ b/changelog.md @@ -1,9 +1,9 @@ -Upcoming (TBD) -============== +Upcoming Release (TBD) +====================== Features -------- - +* Added explicit error handle to get_password_from_file with EAFP. Internal -------- diff --git a/mycli/AUTHORS b/mycli/AUTHORS index 7149be51..8de51691 100644 --- a/mycli/AUTHORS +++ b/mycli/AUTHORS @@ -15,6 +15,7 @@ Contributors: * Abirami P * Adam Chainz * Aljosha Papsch + * Allrob * Andy Teijelo Pérez * Angelo Lupo * Artem Bezsmertnyi diff --git a/mycli/main.py b/mycli/main.py index c5963a7f..1755be90 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -85,6 +85,9 @@ SUPPORT_INFO = "Home: http://mycli.net\n" "Bug tracker: https://github.com/dbcli/mycli/issues" +class PasswordFileError(Exception): + """Base exception for errors related to reading password files.""" + pass class MyCli(object): default_prompt = "\\t \\u@\\h:\\d> " @@ -536,14 +539,19 @@ def _connect(): sys.exit(1) def get_password_from_file(self, password_file): - password_from_file = None if password_file: - if (os.path.isfile(password_file) or stat.S_ISFIFO(os.stat(password_file).st_mode)) and os.access(password_file, os.R_OK): + try: with open(password_file) as fp: - password_from_file = fp.readline() - password_from_file = password_from_file.rstrip().lstrip() - - return password_from_file + password = fp.readline().strip() + return password + except FileNotFoundError: + raise PasswordFileError(f"Password file '{password_file}' not found") from None + except PermissionError: + raise PasswordFileError(f"Permission denied reading password file '{password_file}'") from None + except IsADirectoryError: + raise PasswordFileError(f"Path '{password_file}' is a directory, not a file") from None + except Exception as e: + raise PasswordFileError(f"Error reading password file '{password_file}': {str(e)}") from None def handle_editor_command(self, text): r"""Editor command is any query that is prefixed or suffixed by a '\e'.