Skip to content

Commit

Permalink
server: Improve error handling for admin accounts
Browse files Browse the repository at this point in the history
Instead of the assertion we had before, let's throw a WebRobotError
encoding exactly what has failed. This should give a more helpful error
message.

For example now if server.admin.create('xxxxxxxx') is used:

hetzner.WebRobotError: Unable to create admin account:
    Mindestens ein Gro?buchstabe, Mindestens eine Zahl oder ein
    Sonderzeichen

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
  • Loading branch information
aszlig committed Dec 20, 2016
1 parent 3238d0a commit ca2d131
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions hetzner/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
except ImportError:
from urllib.parse import urlencode

from hetzner import RobotError
from hetzner import RobotError, WebRobotError
from hetzner.rdns import ReverseDNS, ReverseDNSManager
from hetzner.reset import Reset
from hetzner.util import addr, scraping
Expand Down Expand Up @@ -212,13 +212,26 @@ def create(self, passwd=None):
}

if not self.exists:
failmsg = "Unable to create admin account"
path = '/server/adminCreate/id/{0}'.format(self._serverid)
else:
failmsg = "Unable to update admin account password"
path = '/server/adminUpdate'
data['id'] = self._serverid

response = self._scraper.request(path, data)
assert "msgbox_success" in response.read().decode('utf-8')
data = response.read().decode('utf-8')
if "msgbox_success" not in data:
ul_re = re.compile(r'<ul\s+class="error_list">(.*?)</ul>',
re.DOTALL)
li_re = re.compile(r'<li>\s*([^<]*?)\s*</li>')
ul_match = ul_re.search(data)
if ul_match is not None:
errors = [error.group(1)
for error in li_re.finditer(ul_match.group(0))]
msg = failmsg + ': ' + ', '.join(errors)
raise WebRobotError(msg)
raise WebRobotError(failmsg)
self.update_info()
self.passwd = passwd
return self.login, self.passwd
Expand Down

0 comments on commit ca2d131

Please sign in to comment.