praw-dev / praw

PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.
http://praw.readthedocs.io/
BSD 2-Clause "Simplified" License
3.47k stars 456 forks source link

praw exception provides no details when flair id is incorrect #1938

Closed endlisnis closed 1 year ago

endlisnis commented 1 year ago

Describe the Bug

When posting to a sub, if an invalid flair id is used, praw throws:

BadRequest('received 400 HTTP response')

With no other information about what went wrong. It took me literally weeks to figure out that it was an incorrect flair id.

Is praw dropping the description of the failure, or is reddit sending literally nothing?

Desired Result

I would expect the exception to say something like "Incorrect or invalid flair id".

Code to reproduce the bug

import praw
import prawcore

def getApi():
    user_agent='linux:weather alert:v3 (by /u/Endlisnis)'
    reddit = praw.Reddit(client_id='...',
                         client_secret='...',
                         user_agent=user_agent,
                         username='...',
                         password='...')
    return reddit

def getSub():
    api = getApi()
    subreddit = f'/r/Montreal'
    s = api.subreddit(subreddit)
    return api, s

def postImage(api, sub, text, image, depth=1):
    submission = sub.submit_image(
        text, image, timeout=30,
        flair_id='7')

def main():
    api, sub = getSub()
    submission = postImage(
        api, sub, "Image with comment", '/home/rolf/weather/leaflet.png')
    submission.reply('Comment on image')

if __name__=='__main__':
    main()

The Reddit() initialization in my code example does not include the following parameters to prevent credential leakage:

client_secret, password, or refresh_token.

Relevant Logs

Traceback (most recent call last):
  File "/home/rolf/weather/redditBug.py", line 40, in <module>
    main()
  File "/home/rolf/weather/redditBug.py", line 35, in main
    submission = postImage(
  File "/home/rolf/weather/redditBug.py", line 28, in postImage
    submission = sub.submit_image(
  File "/home/rolf/weather/p3.10/lib/python3.10/site-packages/praw/util/deprecate_args.py", line 43, in wrapped
    return func(**dict(zip(_old_args, args)), **kwargs)
  File "/home/rolf/weather/p3.10/lib/python3.10/site-packages/praw/models/reddit/subreddit.py", line 1263, in submit_image
    return self._submit_media(
  File "/home/rolf/weather/p3.10/lib/python3.10/site-packages/praw/models/reddit/subreddit.py", line 623, in _submit_media
    self._reddit.post(API_PATH["submit"], data=data)
  File "/home/rolf/weather/p3.10/lib/python3.10/site-packages/praw/util/deprecate_args.py", line 43, in wrapped
    return func(**dict(zip(_old_args, args)), **kwargs)
  File "/home/rolf/weather/p3.10/lib/python3.10/site-packages/praw/reddit.py", line 842, in post
    return self._objectify_request(
  File "/home/rolf/weather/p3.10/lib/python3.10/site-packages/praw/reddit.py", line 739, in _objectify_request
    self.request(
  File "/home/rolf/weather/p3.10/lib/python3.10/site-packages/praw/util/deprecate_args.py", line 43, in wrapped
    return func(**dict(zip(_old_args, args)), **kwargs)
  File "/home/rolf/weather/p3.10/lib/python3.10/site-packages/praw/reddit.py", line 941, in request
    return self._core.request(
  File "/home/rolf/weather/p3.10/lib/python3.10/site-packages/prawcore/sessions.py", line 330, in request
    return self._request_with_retries(
  File "/home/rolf/weather/p3.10/lib/python3.10/site-packages/prawcore/sessions.py", line 266, in _request_with_retries
    raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.BadRequest: received 400 HTTP response

This code has previously worked as intended.

No

Operating System/Environment

Linux Mint 21.1

Python Version

3.10.6

PRAW Version

7.6.1

Prawcore Version

2.3.0

Anything else?

No response

LilSpazJoekp commented 1 year ago

In this case, PRAW does not get any additional info from Reddit to inform the user why the submitting the post failed. As a result, there isn't much PRAW can do other than raise a generic exception.