joeyvanlierop / xkcdbot

A reddit bot that automatically links xkcd comics in the /r/xkcd subreddit 🤖
MIT License
37 stars 20 forks source link

Issue #1 fix #31

Open stefanosporiazis opened 4 years ago

stefanosporiazis commented 4 years ago

Issue reproduction / explanation:

  1. User enters very large comic number (i.e. many digits)
  2. The bot requests comic from xkcd.com
  3. xkcd.com returns 503 response because of large url
  4. response is not None and neither is response.status_code 404, so the bot falsely assumes response contains json data
  5. The bot tries to access json data and an exception is thrown on statement config = response.json()
  6. Exception is caught by try/except block and execution continues so the comment is basically skipped

(note that as of lately I wasn't able to reproduce the issue. xkcd.com seems to return 404 now even when the url is very large, but this could be temporary. Nevertheless, these changes should help avoid any issues with other status codes that might be returned.)

Changes:

  1. Truncated comment body down to 10k characters (this is not the cause of the issue but it could solve similar problems)

  2. Modified if/else block that checked for a None or 404 response to now check for None, or !200 before accessing json data This way config = response.json() will only be run when response is 200

  3. Modified the functions that match comic ids to only return integers. This way comic_ids will only contain integers.

I'm still testing and looking into any changes that need to be done to the test cases.