Skip to content

Commit

Permalink
fix: explicitly chain the exception when raising from 'except' block
Browse files Browse the repository at this point in the history
  • Loading branch information
sleeptightAnsiC committed Mar 24, 2024
1 parent 05bd9c7 commit e58d718
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ue4cli/JsonDataManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def loads(self):
except json.JSONDecodeError as e:
# FIXME: This is the only place outside of Utility class where we use UtilityException.
# Not worth to create new Exception class for only one single case, at least not now.
raise UtilityException(f'failed to load "{str(path)}" due to: ({type(e).__name__}) {str(e)}')
raise UtilityException(f'failed to load "{str(path)}" due to: ({type(e).__name__}) {str(e)}') from e

def getKey(self, key):
"""
Expand Down
22 changes: 12 additions & 10 deletions ue4cli/UnrealManagerBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def setEngineRootOverride(self, rootDir):
try:
self.getEngineVersion()
except:
raise UnrealManagerException('the specified directory does not appear to contain a valid version of the Unreal Engine.')
raise UnrealManagerException('the specified directory does not appear to contain a valid version of the Unreal Engine.') from None

def clearEngineRootOverride(self):
"""
Expand Down Expand Up @@ -96,7 +96,7 @@ def getEngineVersion(self, outputFormat = 'full'):

# Verify that the requested output format is valid
if outputFormat not in formats:
raise UnrealManagerException('unreconised version output format "{}"'.format(outputFormat))
raise UnrealManagerException(f'unreconised version output format "{str(outputFormat)}"')

return formats[outputFormat]

Expand Down Expand Up @@ -176,7 +176,7 @@ def getDescriptor(self, dir):
try:
return self.getPluginDescriptor(dir)
except:
raise UnrealManagerException('could not detect an Unreal project or plugin in the directory "{}"'.format(dir))
raise UnrealManagerException(f'could not detect an Unreal project or plugin in the directory "{str(dir)}"') from None

def isProject(self, descriptor):
"""
Expand Down Expand Up @@ -355,11 +355,11 @@ def buildDescriptor(self, dir=os.getcwd(), configuration='Development', target='

# If the project or plugin is Blueprint-only, there is no C++ code to build
if os.path.exists(os.path.join(dir, 'Source')) == False:
raise UnrealManagerException('Pure Blueprint {}, nothing to build.'.format(descriptorType))
raise UnrealManagerException(f'Pure Blueprint {str(descriptorType)}, nothing to build.')

# Verify that the specified build configuration is valid
if configuration not in self.validBuildConfigurations():
raise UnrealManagerException('invalid build configuration "' + configuration + '"')
raise UnrealManagerException(f'invalid build configuration "{str(configuration)}"')

# Check if the user specified the `-notools` flag to opt out of building Engine tools when working with source builds
unstripped = list(args)
Expand Down Expand Up @@ -408,7 +408,7 @@ def packageProject(self, dir=os.getcwd(), configuration='Shipping', extraArgs=[]

# Verify that the specified build configuration is valid
if configuration not in self.validBuildConfigurations():
raise UnrealManagerException('invalid build configuration "' + configuration + '"')
raise UnrealManagerException(f'invalid build configuration "{str(configuration)}"')

# Strip out the `-NoCompileEditor` flag if the user has specified it, since the Development version
# of the Editor modules for the project are needed in order to run the commandlet that cooks content
Expand Down Expand Up @@ -538,10 +538,12 @@ def listAutomationTests(self, projectFile):
# Detect if the Editor terminated abnormally (i.e. not triggered by `automation quit`)
# In Unreal Engine 4.27.0, the exit method changed from RequestExit to RequestExitWithStatus
if 'PlatformMisc::RequestExit(' not in logOutput.stdout and 'PlatformMisc::RequestExitWithStatus(' not in logOutput.stdout:
raise UnrealManagerException(
'failed to retrieve the list of automation tests!' +
' stdout was: "{}", stderr was: "{}"'.format(logOutput.stdout, logOutput.stderr)
)
Utility.printStderr("Warning: abnormal Editor termination detected!")
Utility.printStderr("printing stdout..")
print(logOutput.stdout)
Utility.printStderr("printing stderr..")
print(logOutput.stderr)
raise UnrealManagerException('failed to retrieve the list of automation tests!')

return sorted(list(tests))

Expand Down
10 changes: 5 additions & 5 deletions ue4cli/Utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def readFile(filename):
with open(filename, 'rb') as f:
return f.read().decode('utf-8')
except OSError as e:
raise UtilityException(f'failed to read file "{str(filename)}" due to: ({type(e).__name__}) {str(e)}')
raise UtilityException(f'failed to read file "{str(filename)}" due to: ({type(e).__name__}) {str(e)}') from e

@staticmethod
def writeFile(filename, data):
Expand All @@ -44,7 +44,7 @@ def writeFile(filename, data):
with open(filename, 'wb') as f:
f.write(data.encode('utf-8'))
except OSError as e:
raise UtilityException(f'failed to write file "{str(filename)}" due to: ({type(e).__name__}) {str(e)}')
raise UtilityException(f'failed to write file "{str(filename)}" due to: ({type(e).__name__}) {str(e)}') from e

@staticmethod
def moveFile(src, dst):
Expand All @@ -54,7 +54,7 @@ def moveFile(src, dst):
try:
shutil.move(src, dst)
except OSError as e:
raise UtilityException(f'failed to move file from "{str(src)}" to "{str(dst)}" due to: ({type(e).__name__}) {str(e)}')
raise UtilityException(f'failed to move file from "{str(src)}" to "{str(dst)}" due to: ({type(e).__name__}) {str(e)}') from e

@staticmethod
def patchFile(filename, replacements):
Expand All @@ -77,7 +77,7 @@ def removeDir(path, ignore_errors=False):
try:
shutil.rmtree(path, ignore_errors)
except OSError as e:
raise UtilityException(f'failed to remove directory "{str(path)}" due to: ({type(e).__name__}) {str(e)}')
raise UtilityException(f'failed to remove directory "{str(path)}" due to: ({type(e).__name__}) {str(e)}') from e

@staticmethod
def makeDirs(name, mode=0o777, exist_ok=False):
Expand All @@ -87,7 +87,7 @@ def makeDirs(name, mode=0o777, exist_ok=False):
try:
os.makedirs(name, mode, exist_ok)
except OSError as e:
raise UtilityException(f'failed to create directory "{str(name)}" due to: ({type(e).__name__}) {str(e)}')
raise UtilityException(f'failed to create directory "{str(name)}" due to: ({type(e).__name__}) {str(e)}') from e

@staticmethod
def forwardSlashes(paths):
Expand Down
2 changes: 1 addition & 1 deletion ue4cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def main():
else:
# FIXME: This is the only place outside of UnrealManager... classes where we use UnrealManagerException.
# Not worth to create new Exception class for only one single case, at least not now.
raise UnrealManagerException('unrecognised command "' + command + '"')
raise UnrealManagerException(f'unrecognised command "{str(command)}"') from None

except (
UnrealManagerException,
Expand Down

0 comments on commit e58d718

Please sign in to comment.