Bird
0
0

What is wrong with this code snippet that handles 429 errors?

medium📝 Debug Q7 of 15
Rest API - HTTP Status Codes

What is wrong with this code snippet that handles 429 errors?

if response.status_code == 429:
    retry_after = response.headers.get('Retry-After')
    if retry_after:
        sleep(int(retry_after))
    retry_request()

Aretry_request() is called even if retry_after is missing, causing immediate retry
BThe status code check should be 500 instead of 429
Csleep function cannot accept integer arguments
DHeaders cannot be accessed with get() method
Step-by-Step Solution
Solution:
  1. Step 1: Analyze flow when Retry-After header is missing

    If retry_after is None, sleep is skipped but retry_request() runs immediately.
  2. Step 2: Understand impact of immediate retry

    Immediate retry may cause repeated 429 errors; better to handle missing header properly.
  3. Final Answer:

    retry_request() is called even if retry_after is missing, causing immediate retry -> Option A
  4. Quick Check:

    Missing Retry-After causes immediate retry, may cause repeated 429 [OK]
Quick Trick: Handle missing Retry-After to avoid immediate retry after 429 [OK]
Common Mistakes:
MISTAKES
  • Ignoring missing Retry-After header
  • Assuming sleep can't take int
  • Wrong status code check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes