From 330ce550f4d31a7a43625537e414e4e9794a8043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= Date: Tue, 20 Jun 2017 14:04:04 +0200 Subject: [PATCH 1/2] RobotError: Fix __repr__()/__str__() issue. Commit 280f8d31 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 280f8d31 actually broke the `str()`, which until now was just `RobotError()`. This commit fixes both these issues. --- hetzner/__init__.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/hetzner/__init__.py b/hetzner/__init__.py index c1c539b..427097f 100644 --- a/hetzner/__init__.py +++ b/hetzner/__init__.py @@ -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 From e233c701abc589967ce1498d28badcda1e7d2b9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= Date: Tue, 20 Jun 2017 14:33:48 +0200 Subject: [PATCH 2/2] Fix 'The user {0} is a dedicated web service user' missing .format() --- hetzner/robot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hetzner/robot.py b/hetzner/robot.py index e9e7984..f909d9e 100644 --- a/hetzner/robot.py +++ b/hetzner/robot.py @@ -86,7 +86,7 @@ def login(self, user=None, passwd=None, force=False): if self.user.startswith("#ws+"): raise WebRobotError("The user {0} is a dedicated web service user " "and cannot be used for scraping the web user " - "interface.") + "interface.".format(self.user)) # This is primarily for getting a first session cookie. response = self.request('/login', xhr=False)