def _handle_auth_error(msg):
"""Handle reraising HTTP authentication errors as something clearer.
If an ``HTTPError`` is raised with status 401 (access denied) in
the body of this context manager, reraise it as an
``AuthenticationError`` instead, with *msg* as its message.
This function adds no round trips to the server.
:param msg: The message to be raised in ``AuthenticationError``.
:type msg: ``str``
**Example**::
with _handle_auth_error("Your login failed."):
... # make an HTTP request
"""
try:
yield
except HTTPError as he:
if he.status == 401:
raise AuthenticationError(msg, he)
else:
raise
评论列表
文章目录