Get short version of error message with requests
<p>I'm using requests and I need a shortened description of the exception,</p>

<pre><code>    try:
        resp = requests.get(url)
    except Exception, e:
        data['error'] = str(e)
</code></pre>

<p>e.g for a connection error, <code>str(e)</code> becomes <code>('Connection aborted.', error(61, 'Connection refused'))</code></p>

<p>I want to retrieve only the <code>Connection aborted.</code> part.</p>

<p>According to the document of <code>Exception</code> class, the <code>e.strerror</code> should work, but <code>print (e.strerror)</code> shows <code>None</code></p>

<p>Any ideas?</p>

<p>The <code>Exception</code> class is the base class of all user-defined exceptions (it a recommandation).</p>

<p>This class inherits the <code>BaseException</code> which has  a <a href="https://docs.python.org/2/library/exceptions.html#exceptions.BaseException" rel="nofollow"><code>args</code></a> attributes.
This attribute is defined as follow:</p>

<blockquote>
  <p><strong>args</strong></p>
  
  <p>The tuple of arguments given to the exception constructor. Some built-in exceptions (like OSError) expect a certain number of arguments and assign a special meaning to the elements of this tuple, while others are usually called only with a single string giving an error message.</p>
</blockquote>

<p>If the error message is in <code>args[0]</code>, you can try:</p>

<pre><code>try:
    resp = requests.get(url)
except Exception as e:
    data['error'] = e.args[0]
</code></pre>

<p>Your are not speaking of the standard exception, because <code>print (e.strerror)</code> should raise <code>AttributeError</code>.</p>

<p>For your answer, you ought to consider the answer of this qestion: <a href="http://stackoverflow.com/questions/16511337/correct-way-to-try-except-using-python-requests-module?answertab=votes#tab-top">Correct way to try/except using Python requests module?</a></p>

<p>All Requests exceptions inherit <a href="http://docs.python-requests.org/en/master/_modules/requests/exceptions/" rel="nofollow"><code>requests.exceptions.RequestException</code></a>, which inherits <code>IOError</code>.</p>

<p>About <code>IOError</code>, the documentation says:</p>

<blockquote>
  <p><strong>exception EnvironmentError</strong></p>
  
  <p>The base class for exceptions that can occur outside the Python system: IOError, OSError. <strong>When exceptions of this type are created with a 2-tuple</strong>, the first item is available on the instanceâs <em>errno</em> attribute (it is assumed to be an error number), and the second item is available on the <em>strerror</em> attribute (it is usually the associated error message). The tuple itself is also available on the args attribute.</p>
</blockquote>

<p>But <code>RequestException</code> doesn't seems to be created with a 2-tuple, so <em>strerror</em> is <code>None</code>.</p>

<p><strong>EDIT: add some examples:</strong></p>

<p>If you have <code>HTTPError</code>, the original message is in <code>args[0]</code>. See this sample of code in <code>requests.models.Response.raise_for_status</code>:</p>

<pre><code>if http_error_msg:
    raise HTTPError(http_error_msg, response=self)
</code></pre>

<p>If you have <code>ConnectionError</code>, the <code>args[0]</code> contains the original error. See this sample in <code>requests.adapters.HTTPAdapter.send</code>:</p>

<pre><code>except (ProtocolError, socket.error) as err:
    raise ConnectionError(err, request=request)
</code></pre>

<p>Itâs the same for <code>ProxyError</code>, <code>SSLError</code>, <code>Timeout</code>: <code>args[0]</code> contains the original error.</p>

<p>...</p>

<p>Browse the source code in the <a href="https://github.com/kennethreitz/requests/tree/master/requests" rel="nofollow">GitHub repo</a>.</p>

<p><strong>One solution could be:</strong></p>

<pre><code>try:
    resp = requests.get(url)
except requests.exceptions.HTTPError as e:
    data['error'] = e.args[0]
except requests.exceptions.RequestException as e:
    cause = e.args[0]
    data['error'] = str(cause.args[0])
</code></pre>

python python-requests