Skip to content

Consider alternative to using Exception.message to get message text #3270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wtriplett opened this issue Nov 10, 2020 · 1 comment · Fixed by #3272
Closed

Consider alternative to using Exception.message to get message text #3270

wtriplett opened this issue Nov 10, 2020 · 1 comment · Fixed by #3272
Milestone

Comments

@wtriplett
Copy link
Contributor

Summary

Apparently, some Exception classes may not have a .message attribute. This can be more problematic when catching base Exception and then trying to log/warn using a string incorporating the original Exception's .message attribute. Maybe using str(ex) is more compatible, or more specific exception classes?

Example (from master):

try:
proc = sp.Popen(
command, stdout=sp.PIPE, stderr=sp.PIPE, shell=True, env=environ
)
o, e = proc.communicate()
deps = o.rstrip()
except Exception as ex:
deps = '"%s" failed' % command
fmlogger.warning(
"Could not get dependencies of %s. Error:\n%s", name, ex.message
)
return deps

will cause a throw exception if an OSError is caught, since OSError does not have a .message attribute (at least in python 3.8.3)

Actual behavior

Python 3.8.3 (default, May 19 2020, 18:47:26) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = OSError('darn')
>>> a.message
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'OSError' object has no attribute 'message'

Expected behavior

>>> str(a)
'darn'
@effigies
Copy link
Member

I agree, let's move to str.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants