PRAW how to catch a "received 401 HTTP response"

Viewed 1445

I'm purposely trying to authenticate with wrong credentials to the reddit API using PRAW. I can see on my console that I'm getting "received 401 HTTP response" error.

Traceback (most recent call last):
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/bob/Sites/django/myproj/src/blog/views.py", line 246, in syncMyPosts
    thisimage.remove_category = submission.removed_by_category
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/models/reddit/base.py", line 33, in __getattr__
    self._fetch()
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/models/reddit/submission.py", line 591, in _fetch
    data = self._fetch_data()
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/models/reddit/submission.py", line 588, in _fetch_data
    return self._reddit.request("GET", path, params)
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/reddit.py", line 726, in request
    return self._core.request(
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 329, in request
    return self._request_with_retries(
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 227, in _request_with_retries
    response, saved_exception = self._make_request(
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 185, in _make_request
    response = self._rate_limiter.call(
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/rate_limit.py", line 35, in call
    kwargs["headers"] = set_header_callback()
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 282, in _set_header_callback
    self._authorizer.refresh()
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/auth.py", line 349, in refresh
    self._request_token(
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/auth.py", line 149, in _request_token
    response = self._authenticator._post(url, **data)
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/auth.py", line 32, in _post
    raise ResponseException(response)
prawcore.exceptions.ResponseException: received 401 HTTP response

I would rather catch this error / exception and show a friendly message. Having a hard time understanding how to use the try catch exception.

Pretty new to python & Django.

Thanks in advance

1 Answers

Here's a short sample that does what you want, using Python's try and except. We import ResponseException because that is what we want to catch.

import praw
from prawcore.exceptions import ResponseException

reddit = praw.Reddit(
    username="wrong_username",
    password="wrong password",
    client_id="bad client ID",
    client_secret="bad secret",
    user_agent="failing authentication",
)

try:
    print("Authenticated as {}".format(reddit.user.me()))
except ResponseException:
    print("Something went wrong during authentication")

We surround the code that will throw the exception (which is reddit.user.me() in this case) with the try and except blocks, so that we can specify the alternate behavior when an exception occurs.

Related