Skip to content

Conversation

@matheusgalvao1
Copy link
Contributor

Bug Fix: Prevent 404 Error Responses from Corrupting Quiz Answers

Problem

When the quiz application starts, it fetches /answers.json to load any previously saved answers. If this file doesn't exist (common on first run), the server returns a 404 response with JSON content:

{"error": "Not Found"}

The frontend incorrectly treated this error response as valid answer data, causing corrupted answers.json files:

{
  "error": "Not Found",                    ← ❌ Server error mixed with quiz data
  "civilwar": {
    "value": "1861-1865",
    "correctAnswer": "1861-1865", 
    "isCorrect": true
  },
  "capital": {
    "value": "Paris",
    "correctAnswer": "Paris",
    "isCorrect": true
  }
}

This corruption caused the format_answers.py script to crash:

AttributeError: 'str' object has no attribute 'get'

Root Cause

The frontend fetch logic was not checking response.ok before parsing JSON:

fetch('/answers.json')
    .then(response => response.json())  // ❌ Parses error responses as valid data
    .catch(() => ({}))  // Only catches network/parsing errors, not 404s

Solution

Modified the fetch logic to properly handle HTTP error responses:

fetch('/answers.json')
    .then(response => {
        if (!response.ok) {
            // If file doesn't exist (404) or other error, return empty object
            return {};
        }
        return response.json();
    })
    .catch(() => ({}))

Testing

  • ✅ Deleted answers.json and verified clean startup without error pollution
  • ✅ Confirmed quiz answers save correctly without error objects
  • ✅ Verified format_answers.py runs successfully after taking quiz
  • ✅ No functional changes to quiz behavior or server logic

Prevent server error responses from being treated as valid answer data
when answers.json doesn't exist on initial app load.
Copy link
Contributor

@BrianGenisio BrianGenisio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@matheusgalvao1 matheusgalvao1 merged commit ddaa4a0 into main Jun 2, 2025
@matheusgalvao1 matheusgalvao1 deleted the bugfix/fix-answers-404-error-handling branch June 2, 2025 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants