From 536e87d14c56ecc0209a72455af6d614a913c3c9 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 22 Jun 2014 12:23:07 +0000 Subject: [PATCH] Handle HTTP errors as early as possible. --- src/ddns/providers.py | 22 +--------------------- src/ddns/system.py | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/ddns/providers.py b/src/ddns/providers.py index ece4b37..a729a22 100644 --- a/src/ddns/providers.py +++ b/src/ddns/providers.py @@ -190,19 +190,9 @@ class DDNSProviderAllInkl(DDNSProvider): url = "http://dyndns.kasserver.com" def update(self): - # There is no additional data required so we directly can # send our request. - try: - # Send request to the server. - response = self.send_request(self.url, username=self.username, password=self.password) - - # Handle 401 HTTP Header (Authentication Error) - except urllib2.HTTPError, e: - if e.code == 401: - raise DDNSAuthenticationError - - raise + response = self.send_request(self.url, username=self.username, password=self.password) # Get the full response message. output = response.read() @@ -244,10 +234,6 @@ class DDNSProviderDHS(DDNSProvider): if response.code == 200: return - # Handle error codes. - elif response.code == 401: - raise DDNSAuthenticationError - # If we got here, some other update error happened. raise DDNSUpdateError @@ -542,12 +528,6 @@ class DDNSProviderLightningWireLabs(DDNSProvider): if response.code == 200: return - # Handle error codes. - if response.code == 403: - raise DDNSAuthenticationError - elif response.code == 400: - raise DDNSRequestError - # If we got here, some other update error happened. raise DDNSUpdateError diff --git a/src/ddns/system.py b/src/ddns/system.py index afc5a5f..ef525fb 100644 --- a/src/ddns/system.py +++ b/src/ddns/system.py @@ -168,9 +168,22 @@ class DDNSSystem(object): return resp except urllib2.HTTPError, e: + # 400 - Bad request + if e.code == 400: + raise DDNSRequestError(e.reason) + + # 401 - Authorization Required + # 403 - Forbidden + elif e.code in (401, 403): + raise DDNSAuthenticationError(e.reason) + + # 500 - Internal Server Error + elif e.code == 500: + raise DDNSInternalServerError(e.reason) + # 503 - Service Unavailable - if e.code == 503: - raise DDNSServiceUnavailableError + elif e.code == 503: + raise DDNSServiceUnavailableError(e.reason) # Raise all other unhandled exceptions. raise -- 2.39.2