Skip to content

Commit

Permalink
RobotError: Correctly initialize exception message
Browse files Browse the repository at this point in the history
Regression introduced by 788b3fd.

While setting self.message is documented for setting user-defined
exceptions, it might be a misunderstanding by me and it's *not* about
the printed result of the exception but about setting custom attributes
for an exception.

After digging a bit deeper on this, I found that both Python 2.7 and 3.x
simply accumulate all arguments passed to __init__() to self.args, see:

https://github.com/python/cpython/blob/2.7/Objects/exceptions.c#L65
https://github.com/python/cpython/blob/3.6/Objects/exceptions.c#L66

Testing this with the following example leads to the expected result:

    class Foo(Exception):
        def __init__(self):
            super(Foo, self).__init__(111, "bar")

    raise Foo()

Result for Python 2.7.13:

    Traceback (most recent call last):
      File "exctest.py", line 5, in <module>
        raise Foo()
    __main__.Foo: (111, 'bar')

Result for Python 3.6.1:

    Traceback (most recent call last):
      File "exctest.py", line 5, in <module>
        raise Foo()
    __main__.Foo: (111, 'bar')

So the self.args attribute is just passed to repr() and/or str() and
that's about it.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
Issue: #23
Cc: @nh2
  • Loading branch information
aszlig committed Jul 25, 2017
1 parent 2666376 commit c7181fd
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions hetzner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
class RobotError(Exception):
def __init__(self, message, status=None):
if status is None:
self.message = message
else:
self.message = "{0} ({1})".format(message, status)
self.status = status
if status is not None:
message = "{0} ({1})".format(message, status)
super(RobotError, self).__init__(message)


class ManualReboot(Exception):
Expand Down

0 comments on commit c7181fd

Please sign in to comment.