[BACKEND] More explicit error message for PermissionError (#736)

Do not print the full stack trace when a PermissionError occurs
This commit is contained in:
Jesse Bannon 2023-09-21 23:06:57 -07:00 committed by GitHub
parent 3c87dc9f8e
commit 3d9c35519d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View file

@ -221,6 +221,13 @@ class Logger:
# Log validation exceptions as-is
if isinstance(exception, ValidationException):
logger.error(str(exception))
# Log permission errors explicitly
elif isinstance(exception, PermissionError):
logger.error(
"A permission error occurred:\n%s\n"
"The user running ytdl-sub must have permission to this file/directory.",
str(exception),
)
# For other uncaught errors, log as bug:
else:
logger.exception("An uncaught error occurred:")

View file

@ -69,6 +69,21 @@ def test_main_uncaught_error(capsys, mock_sys_exit, expected_uncaught_error_mess
assert mock_error.call_args.args[2] == Logger.debug_log_filename()
def test_main_permission_error(capsys, mock_sys_exit, expected_uncaught_error_message):
permission_error = PermissionError("test")
with mock_sys_exit(expected_exit_code=1), patch(
"src.ytdl_sub.main._main", side_effect=permission_error
), patch.object(logging.Logger, "error") as mock_error:
main()
assert mock_error.call_count == 1
assert mock_error.call_args.args[0] == (
"A permission error occurred:\n%s\n"
"The user running ytdl-sub must have permission to this file/directory."
)
assert mock_error.call_args.args[1] == "test"
def test_args_after_sub_work(mock_sys_exit):
with mock_sys_exit(expected_exit_code=0), patch.object(
sys,