Skip to content

Commit 5e5d736

Browse files
committed
Handle bad HTTP responses properly
urlopen raises a HTTPError upon failure, which didn't go through handleresponseerror(). The original handling is left in case there are obscure conditions where urlopen doesn't raise a HTTPError.
1 parent 1ed6857 commit 5e5d736

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

pyPostcode/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
try:
1111
from urllib.request import urlopen, Request # for Python 3
12+
from urllib.error import HTTPError
1213
except ImportError:
13-
from urllib2 import urlopen, Request # for Python 2
14+
from urllib2 import urlopen, Request, HTTPError # for Python 2
1415

1516
import json
1617
import logging
@@ -63,9 +64,12 @@ def request(self, path=None):
6364
"X-Api-Key": self.api_key,
6465
}
6566

66-
result = urlopen(Request(
67-
self.url + path, headers=headers,
68-
))
67+
try:
68+
result = urlopen(Request(
69+
self.url + path, headers=headers,
70+
))
71+
except HTTPError as err:
72+
self.handleresponseerror(err.code)
6973

7074
if result.getcode() != 200:
7175
self.handleresponseerror(result.getcode())

0 commit comments

Comments
 (0)