Skip to content

Commit

Permalink
🔊 no more silent exceptions + more specific error type
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu committed Jan 31, 2023
1 parent 66dc283 commit 7739093
Show file tree
Hide file tree
Showing 35 changed files with 114 additions and 91 deletions.
3 changes: 2 additions & 1 deletion developer/bake_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from tank.descriptor import create_descriptor, is_descriptor_version_missing
from tank.descriptor.errors import TankDescriptorError
from tank.bootstrap import constants as bootstrap_constants
from shotgun_api3 import ShotgunError
import functools

from utils import (
Expand Down Expand Up @@ -293,7 +294,7 @@ def main():
# make sure we are properly connected
try:
sg_connection.find_one("HumanUser", [])
except Exception as e:
except ShotgunError as e:
logger.error("Could not communicate with ShotGrid: %s" % e)
return 3

Expand Down
3 changes: 2 additions & 1 deletion hooks/get_current_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ def execute(self, **kwargs):

pwd_entry = pwd.getpwuid(os.geteuid())
return pwd_entry[0]
except:
except Exception:
self.logger.debug("GetCurrentLogin hook failed.", exc_info=True)
return None
3 changes: 2 additions & 1 deletion python/tank/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ def documentation_url(self):
data = str(data.get("documentation_url"))
if data == "":
data = None
except Exception:
except Exception as e:
log.debug("Cant get documentation url: %s" %e)
data = None

return data
Expand Down
3 changes: 2 additions & 1 deletion python/tank/authentication/interactive_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def _get_current_os_user():

pwd_entry = pwd.getpwuid(os.geteuid())
return pwd_entry[0]
except:
except Exception as e:
logger.debug("Cant get current user: %s" %e)
return None


Expand Down
2 changes: 1 addition & 1 deletion python/tank/authentication/session_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,6 @@ def generate_session_token(hostname, login, password, http_proxy, auth_token=Non
# If the error message is empty, like httplib.HTTPException, convert
# the class name to a string
if len(str(e)) == 0:
raise Exception("Unknown error: %s" % type(e).__name__)
raise type(e)("Unknown error: %s" % type(e).__name__)
else:
raise
2 changes: 1 addition & 1 deletion python/tank/commands/cache_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _run(self, log):

try:
fh = open(pickle_path, "wb")
except Exception as e:
except OSError as e:
raise TankError("Unable to open '%s' for writing: %s" % (pickle_path, e))

try:
Expand Down
2 changes: 1 addition & 1 deletion python/tank/commands/clone_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def _do_clone(
fh.write("# End of file.\n")
fh.close()

except Exception as e:
except OSError as e:
raise TankError("Could not create file system structure: %s" % e)

# lastly, update the pipeline_configuration.yml file
Expand Down
2 changes: 1 addition & 1 deletion python/tank/commands/console_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def _get_configuration_recursive(
param_name,
answer,
)
except Exception as e:
except TankError as e:
log.error("Validation failed: %s" % e)
else:
input_valid = True
Expand Down
4 changes: 2 additions & 2 deletions python/tank/commands/dump_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _run(self, log, params):
log.info(
"Environment written to: %s" % (os.path.abspath(params["file"]))
)
except Exception as e:
except TankError as e:
import traceback

traceback.print_exc()
Expand Down Expand Up @@ -244,7 +244,7 @@ def _get_file_handle(self, params):
)
try:
fh = open(path, "w")
except Exception as e:
except OSError as e:
raise TankError(
"Unable to open file: %s\n" " Error reported: %s" % (path, e)
)
Expand Down
4 changes: 2 additions & 2 deletions python/tank/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def _run(self, log, env_name, engine_instance_name, app_name, preserve_yaml):
env_name, writable=True
)
env.set_yaml_preserve_mode(preserve_yaml)
except Exception as e:
except TankError as e:
raise TankError(
"Environment '%s' could not be loaded! Error reported: %s"
% (env_name, e)
Expand Down Expand Up @@ -528,7 +528,7 @@ def _run(self, log, env_name, engine_name, preserve_yaml):
env_name, writable=True
)
env.set_yaml_preserve_mode(preserve_yaml)
except Exception as e:
except TankError as e:
raise TankError(
"Environment '%s' could not be loaded! Error reported: %s"
% (env_name, e)
Expand Down
8 changes: 4 additions & 4 deletions python/tank/commands/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def _run(self, log):
log.debug("Deleting cache file %s..." % full_path)
try:
os.remove(full_path)
except:
log.warning("Could not delete cache file '%s'!" % full_path)
except OSError as e:
log.warning("Could not delete cache file '%s'! // %s" % (full_path, e))

log.info("The SG menu cache has been cleared.")

Expand Down Expand Up @@ -137,7 +137,7 @@ def run_interactive(self, log, args):
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
except:
pass
except Exception as e:
msg.append(e)

code.interact(banner="\n".join(msg), local=tk_locals)
6 changes: 3 additions & 3 deletions python/tank/commands/move_pc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _cleanup_old_location(self, log, path):
log.debug("Removing %s..." % full_path)
try:
os.remove(full_path)
except Exception as e:
except OSError as e:
log.warning(
"Could not delete file %s. Error Reported: %s"
% (full_path, e)
Expand All @@ -68,7 +68,7 @@ def _cleanup_old_location(self, log, path):
log.debug("Deleting folder %s..." % full_path)
try:
os.rmdir(full_path)
except Exception as e:
except OSError as e:
log.warning(
"Could not remove folder %s. Error Reported: %s"
% (full_path, e)
Expand Down Expand Up @@ -272,7 +272,7 @@ def run_interactive(self, log, args):
fh.write("# End of file.\n")
fh.close()

except Exception as e:
except OSError as e:
raise TankError(
"Could not copy configuration! This may be because of system "
"permissions or system setup. This configuration will "
Expand Down
6 changes: 3 additions & 3 deletions python/tank/commands/push_pc.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ def run_interactive(self, log, args):
raise TankError("Aborted by user.")
try:
target_pc_id = int(answer)
except:
raise TankError("Please enter a number!")
except ValueError as e:
raise TankError("Please enter a number! // %s" % e)

self._run(
log,
Expand Down Expand Up @@ -230,7 +230,7 @@ def _run(self, log, target_id, use_symlink=False):
for env_name in self.tk.pipeline_configuration.get_environments():
try:
env = self.tk.pipeline_configuration.get_environment(env_name)
except Exception as e:
except TankError as e:
raise TankError(
"Failed to load environment %s,"
" run 'tank validate' for more details, got error: %s"
Expand Down
4 changes: 2 additions & 2 deletions python/tank/commands/setup_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ def _select_project(self, log, sg, show_initialized_projects):
raise TankError("Aborted by user.")
try:
project_id = int(answer)
except:
raise TankError("Please enter a number!")
except ValueError as e:
raise TankError("Please enter a number! // %s" % e)

if project_id not in [x["id"] for x in projs]:
raise TankError("Id %d was not found in the list of projects!" % project_id)
Expand Down
2 changes: 1 addition & 1 deletion python/tank/commands/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def run_interactive(self, log, args):
env_name, writable=True
)
env.set_yaml_preserve_mode(preserve_yaml)
except Exception as e:
except TankError as e:
raise TankError(
"Environment '%s' could not be loaded! Error reported: %s"
% (env_name, e)
Expand Down
2 changes: 1 addition & 1 deletion python/tank/descriptor/io_descriptor/appstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def get_changelog(self):
summary = sg_version_data.get("description")
url = sg_version_data.get("sg_detailed_release_notes").get("url")
except Exception:
pass
log.exception("Cant get changelog.")
return (summary, url)

def _download_local(self, destination_path):
Expand Down
3 changes: 2 additions & 1 deletion python/tank/descriptor/io_descriptor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ def _find_latest_tag_by_pattern(self, version_numbers, pattern):
for version_num in version_numbers:
try:
version_split = list(map(int, version_num[1:].split(".")))
except Exception:
except Exception as e:
log.debug(e)
# this git tag is not on the expected form vX.Y.Z where X Y and Z are ints. skip.
continue

Expand Down
8 changes: 4 additions & 4 deletions python/tank/descriptor/io_descriptor/downloadable.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def open_write_location(self):
target_parent = os.path.dirname(target)
try:
filesystem.ensure_folder_exists(target_parent)
except Exception as e:
except OSError as e:
if not os.path.exists(target_parent):
log.error("Failed to create directory %s: %s" % (target_parent, e))
raise TankDescriptorIOError(
Expand All @@ -104,7 +104,7 @@ def open_write_location(self):
# download completed without issue. Now create settings folder
metadata_folder = self._get_metadata_folder(temporary_path)
filesystem.ensure_folder_exists(metadata_folder)
except Exception as e:
except OSError as e:
# something went wrong during the download, remove the temporary files.
log.error(
"Failed to download into path %s: %s. Attempting to remove it."
Expand Down Expand Up @@ -141,7 +141,7 @@ def open_write_location(self):
% target
)

except Exception as e:
except OSError as e:

# if the target path already exists, this means someone else is either
# moving things right now or have moved it already, so we are ok.
Expand Down Expand Up @@ -188,7 +188,7 @@ def open_write_location(self):
filesystem.safe_delete_folder(temporary_path)
move_succeeded = True

except Exception as e:
except OSError as e:
# something during the copy went wrong. Attempt to roll back the target
# so we aren't left with any corrupt bundle cache items.
if os.path.exists(target):
Expand Down
7 changes: 4 additions & 3 deletions python/tank/descriptor/io_descriptor/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def _can_hide_terminal():
subprocess.STARTF_USESHOWWINDOW
subprocess.SW_HIDE
return True
except Exception:
except AttributeError as e:
log.debug("Terminal cant be hidden: %s" % e)
return False


Expand Down Expand Up @@ -131,7 +132,7 @@ def _clone_then_execute_git_commands(
log.debug("Checking that git exists and can be executed...")
try:
output = _check_output(["git", "--version"])
except:
except SubprocessCalledProcessError:
log.exception("Unexpected error:")
raise TankGitError(
"Cannot execute the 'git' command. Please make sure that git is "
Expand Down Expand Up @@ -291,7 +292,7 @@ def has_remote_access(self):
# clone repo into temp folder
self._tmp_clone_then_execute_git_commands([], depth=1)
log.debug("...connection established")
except Exception as e:
except (OSError, SubprocessCalledProcessError) as e:
log.debug("...could not establish connection: %s" % e)
can_connect = False
return can_connect
Expand Down
9 changes: 5 additions & 4 deletions python/tank/descriptor/io_descriptor/git_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import os
import copy

from .git import IODescriptorGit, _check_output
from .git import IODescriptorGit, _check_output, TankGitError
from ..errors import TankDescriptorError
from ... import LogManager

from tank_vendor import six
from ...util.process import SubprocessCalledProcessError

log = LogManager.get_logger(__name__)

Expand Down Expand Up @@ -119,7 +120,7 @@ def _is_latest_commit(self, version, branch):
log.debug("Checking if the version is pointing to the latest commit...")
try:
output = _check_output(["git", "ls-remote", self._path, branch])
except:
except SubprocessCalledProcessError:
log.exception("Unexpected error:")
raise TankGitError(
"Cannot execute the 'git' command. Please make sure that git is "
Expand Down Expand Up @@ -168,7 +169,7 @@ def _download_local(self, destination_path):
ref=self._branch,
is_latest_commit=is_latest_commit,
)
except Exception as e:
except (TankGitError, OSError, SubprocessCalledProcessError) as e:
raise TankDescriptorError(
"Could not download %s, branch %s, "
"commit %s: %s" % (self._path, self._branch, self._version, e)
Expand Down Expand Up @@ -215,7 +216,7 @@ def get_latest_version(self, constraint_pattern=None):
]
git_hash = self._tmp_clone_then_execute_git_commands(commands)

except Exception as e:
except (TankGitError, OSError, SubprocessCalledProcessError) as e:
raise TankDescriptorError(
"Could not get latest commit for %s, "
"branch %s: %s" % (self._path, self._branch, e)
Expand Down
7 changes: 4 additions & 3 deletions python/tank/descriptor/io_descriptor/git_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import copy
import re

from .git import IODescriptorGit
from .git import IODescriptorGit, TankGitError
from ..errors import TankDescriptorError
from ... import LogManager

from tank_vendor import six
from ...util.process import SubprocessCalledProcessError

log = LogManager.get_logger(__name__)

Expand Down Expand Up @@ -145,7 +146,7 @@ def _download_local(self, destination_path):
self._clone_then_execute_git_commands(
destination_path, [], depth=1, ref=self._version
)
except Exception as e:
except (TankGitError, OSError, SubprocessCalledProcessError) as e:
raise TankDescriptorError(
"Could not download %s, " "tag %s: %s" % (self._path, self._version, e)
)
Expand Down Expand Up @@ -222,7 +223,7 @@ def _fetch_tags(self):
if m:
git_tags.append(m.group(1))

except Exception as e:
except (TankGitError, OSError, SubprocessCalledProcessError) as e:
raise TankDescriptorError(
"Could not get list of tags for %s: %s" % (self._path, e)
)
Expand Down
6 changes: 3 additions & 3 deletions python/tank/folder/folder_types/listfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ def _create_folders_impl(self, io_receiver, parent_path, sg_data):
chunks = self._field_name.split(".")
entity_type = chunks[1]
field_name = chunks[2]
except:
except IndexError as e:
msg = (
"Folder creation error: Cannot resolve the field expression %s."
% self._field_name
"Folder creation error: Cannot resolve the field expression %s: %s"
% (self._field_name, e)
)
raise TankError(msg)

Expand Down
4 changes: 2 additions & 2 deletions python/tank/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,10 @@ def some_method(self):

try:
engine = self.__parent.engine
except:
except Exception as e:
raise TankError(
"Cannot load framework %s for %r - it does not have a "
"valid engine property!" % (framework_instance_name, self.__parent)
"valid engine property! // %s" % (framework_instance_name, self.__parent, e)
)

return framework.load_framework(
Expand Down
Loading

0 comments on commit 7739093

Please sign in to comment.