3

I can't for the life of me figure out why I can't catch this exception.

Looking here at this guide.

def get_team_names(get_team_id_url, team_id):
    print(get_team_id_url + team_id)
    try:
        response = urllib.request.urlopen(get_team_id_url + team_id)
    except urllib.error.HTTPError as e:
        print(e.code)
        print(e.read()) 
    except urllib.error.URLError as e:
        print(e.code)
        print(e.read()) 

exception:

Traceback (most recent call last):
  File "queue_cleaner_main.py", line 60, in <module>
    sys.exit(main())
  File "queue_cleaner_main.py", line 57, in main
    team_names_to_contact = queue_cleaner_functions.get_team_names(SERVICE_NOW_TEAM_NAME_URL, team[2])
  File "D:\oppssup\old_job\queue_cleaner_functions.py", line 132, in get_team_names
    response = urllib.request.urlopen(get_team_id_url + team_id)
  File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 455, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 473, in _open
    '_open', req)
  File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 1202, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python34\lib\urllib\request.py", line 1177, in do_open
    r = h.getresponse()
  File "C:\Python34\lib\http\client.py", line 1172, in getresponse
    response.begin()
  File "C:\Python34\lib\http\client.py", line 351, in begin
    version, status, reason = self._read_status()
  File "C:\Python34\lib\http\client.py", line 321, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: ''

In addition, my entire method seems to be very clunky. I cannot figure out an elegant way to handle exceptions, including a case where no result is returned.

def get_team_names(get_team_id_url, team_id):
    print(get_team_id_url + team_id)
    try:
        response = urllib.request.urlopen(get_team_id_url + team_id)
    except Exception as e:
        #print(e.code)
        #print(e.read())
        print('shit')
    #print(response.read().decode('utf8'))
    r_json = json.loads(response.read().decode('utf8'))
    print(r_json['result'])
    for i in r_json['result']:
        print(i['group'])

Exception:

Traceback (most recent call last):
  File "queue_cleaner_main.py", line 60, in <module>
    sys.exit(main())
  File "queue_cleaner_main.py", line 57, in main
    team_names_to_contact = queue_cleaner_functions.get_team_names(SERVICE_NOW_TEAM_NAME_URL, team[2])
  File "D:\oppssup\old_job\queue_cleaner_functions.py", line 141, in get_team_names
    r_json = json.loads(response.read().decode('utf8'))
UnboundLocalError: local variable 'response' referenced before assignment
Vemonus
  • 868
  • 1
  • 16
  • 28
Justin S
  • 1,409
  • 4
  • 22
  • 38

1 Answers1

6

This http.client.BadStatusLine is a subclass of http.client.HTTPException. I think if you do:

try:
    response = urllib.request.urlopen(get_team_id_url + team_id)
except http.client.HTTPException as e:
    print(e)

then you shouldn't have problem catching it. However, what caused it is perhaps what you should concern.

kenorb
  • 155,785
  • 88
  • 678
  • 743
ljk321
  • 16,242
  • 7
  • 48
  • 60
  • Hi skyline thanks for your respone. When i add that line i get the below error File "D:\oppssup\old_job\queue_cleaner_functions.py", line 112, in get_team_name except http.client.HTTPException as e: NameError: name 'http' is not defined – Justin S May 04 '15 at 05:17
  • Did you import `http`? – ljk321 May 04 '15 at 10:11
  • 2
    thanks mate that fixed it. Why do i need to import http when I dont even use it, only to catch an exception? I thought that exception handling is handled only by each imported module that is used, not having to import specific modules just to handle exceptions in another module? – Justin S May 05 '15 at 02:21
  • I think this may be some kind of design defect of `urllib` in python3. I don't like the need of importing other libs, either. – ljk321 May 05 '15 at 02:40
  • Helped me more than 3 years later. Thanks mate! py3.6 here. – x3l51 Sep 06 '18 at 13:25