Repository: python-telegram-bot/python-telegram-bot

Issue:
httplib exceptions
Running my bot continuously, I sometimes get rare exceptions. This traceback was with Python 2.7  and python-telegram-bot 3.2:

```
  File "/home/rahiel/BismillahBot/bismillah.py", line 99, in send_quran  
    bot.sendMessage(chat_id=chat_id, text=text)
  File "/home/rahiel/BismillahBot/venv/local/lib/python2.7/site-packages/telegram/bot.py", line 127, in decorator  
    result = func(self, *args, **kwargs)  
  File "/home/rahiel/BismillahBot/venv/local/lib/python2.7/site-packages/telegram/bot.py", line 159, in decorator  
    result = request.post(url, data)  
  File "/home/rahiel/BismillahBot/venv/local/lib/python2.7/site-packages/telegram/utils/request.py", line 67, in decorator  
    return func(*args, **kwargs)  
  File "/home/rahiel/BismillahBot/venv/local/lib/python2.7/site-packages/telegram/utils/request.py", line 140, in post  
    result = urlopen(request, timeout=timeout).read()  
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen  
    return _opener.open(url, data, timeout)  
  File "/usr/lib/python2.7/urllib2.py", line 404, in open  
    response = self._open(req, data)  
  File "/usr/lib/python2.7/urllib2.py", line 422, in _open  
    '_open', req)  
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain  
    result = func(*args)  
  File "/usr/lib/python2.7/urllib2.py", line 1222, in https_open  
    return self.do_open(httplib.HTTPSConnection, req)  
  File "/usr/lib/python2.7/urllib2.py", line 1187, in do_open  
    r = h.getresponse(buffering=True)  
  File "/usr/lib/python2.7/httplib.py", line 1051, in getresponse  
    response.begin()  
  File "/usr/lib/python2.7/httplib.py", line 415, in begin  
    version, status, reason = self._read_status()  
  File "/usr/lib/python2.7/httplib.py", line 379, in _read_status  
    raise BadStatusLine(line)  
httplib.BadStatusLine: ''  
```

Using version 2.9 of the library I got this traceback in the past:

```
  File "/home/rahiel/BismillahBot/bismillah.py", line 122, in upload
    v = bot.sendAudio(audio=f, **kwargs)["audio"]["file_id"]
  File "/home/rahiel/BismillahBot/venv/local/lib/python2.7/site-packages/telegram/bot.py", line 126, in decorator
    result = func(self, *args, **kwargs)
  File "/home/rahiel/BismillahBot/venv/local/lib/python2.7/site-packages/telegram/bot.py", line 158, in decorator
    result = request.post(url, data)
  File "/home/rahiel/BismillahBot/venv/local/lib/python2.7/site-packages/telegram/utils/request.py", line 108, in post
    message = _parse(error.read())
  File "/usr/lib/python2.7/socket.py", line 351, in read
    data = self._sock.recv(rbufsize)
  File "/usr/lib/python2.7/httplib.py", line 549, in read
    return self._read_chunked(amt)
  File "/usr/lib/python2.7/httplib.py", line 603, in _read_chunked
    raise IncompleteRead(''.join(value))
httplib.IncompleteRead: IncompleteRead(0 bytes read)
```

Maybe we should catch these exceptions and reraise them as a TelegramError for http errors? I was puzzled by the second traceback, because I was getting it frequently, but that stopped since I updated the library.



Discussion:
@tsnoam Maybe you want to take a look at that? :)

@jh0ker sorry for the latency. i will try to take a look at it tonight.

these two exceptions seem to be caused when there is some communication error with the telegram server. 


Resolving patch:
diff --git a/telegram/utils/request.py b/telegram/utils/request.py
--- a/telegram/utils/request.py
+++ b/telegram/utils/request.py
@@ -25,6 +25,13 @@
 import socket
 from ssl import SSLError
 
+try:
+    # python2
+    from httplib import HTTPException
+except ImportError:
+    # python3
+    from http.client import HTTPException
+
 try:
     from urllib.request import urlopen, urlretrieve, Request
     from urllib.error import HTTPError
@@ -82,6 +89,8 @@ def decorator(*args, **kwargs):
                 raise TelegramError("Timed out")
 
             raise TelegramError(str(error))
+        except HTTPException as error:
+            raise TelegramError('HTTPException: {0!r}'.format(error))
 
     return decorator
 


Test patch: