Skip to content

Commit

Permalink
RobotError: Fix __repr__()/__str__() issue.
Browse files Browse the repository at this point in the history
Commit 280f8d3 already tried to improve this, but the reliable way
to render an exception message is to pass it as the first argument
to the `Exception` superclass constructor; see:
https://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python

This guarantees that both `repr()` and `str()` are set as expected.

Until now, for example, `repr()` did not actually render the exception
name "RobotError", which was confusing, as all other Python exceptions
do that, and the docs say that it should.
Similarly, the fix from 280f8d3 actually broke the `str()`, which
until now was just `RobotError()`.
This commit fixes both these issues.
  • Loading branch information
nh2 committed Jun 20, 2017
1 parent ca2d131 commit 330ce55
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions hetzner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
class RobotError(Exception):
def __init__(self, message, status=None):
self.message = message
formattedMessage = message if status is None else "{0} ({1})".format(message, status)
super(Exception, self).__init__(formattedMessage)
self.status = status

def __str__(self):
if self.status is None:
return self.message
else:
return "{0} ({1})".format(self.message, self.status)


class ManualReboot(Exception):
pass
Expand Down

0 comments on commit 330ce55

Please sign in to comment.