{"record":{"id":"6beec576ff922029","repo":"datawhalechina/hello-agents","slug":"http-error-response-status","errorCode":null,"errorMessage":"HTTP error: ${response.status}","messagePattern":"HTTP error: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Co-creation-projects/afei-GuessWhoAmI/frontend/app.js","lineNumber":51,"sourceCode":"\n    // Play again button (HTML id=\"play-again\")\n    document.getElementById('play-again')\n        .addEventListener('click', () => this.restartGame());\n  }\n\n  // Start new game\n  async startNewGame() {\n    try {\n      this.setStartBtnLoading(true);\n      this.showLoadingOverlay();\n\n      const response = await fetch('http://localhost:8000/api/game/start', {\n        method: 'POST',\n        headers: {'Content-Type': 'application/json'},\n        body: JSON.stringify({})\n      });\n\n      if (!response.ok) throw new Error(`HTTP error: ${response.status}`);\n\n      const result = await response.json();\n\n      if (!result.success) throw new Error(result.error || '启动失败');\n\n      const data = result.data;\n      this.sessionId = data.session_id;\n      this.remainingQuestions = data.max_questions || 20;\n      this.remainingHints = data.max_hints || 3;\n\n      // Switch to game screen\n      document.getElementById('intro-section').classList.add('hidden');\n      document.getElementById('game-section').classList.remove('hidden');\n      document.getElementById('result-modal').classList.add('hidden');\n\n      // Enable controls\n      document.getElementById('user-input').disabled = false;\n      document.getElementById('send-btn').disabled = false;","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/afei-GuessWhoAmI/frontend/app.js#L33-L69","documentation":"Guard in startNewGame(): POST to a HARDCODED http://localhost:8000/api/game/start that throws on any non-2xx status. Because the URL is hardcoded, deploying this frontend anywhere non-local guarantees either CORS failures (which reject fetch entirely) or 4xx/5xx that land here.","triggerScenarios":"Backend not running on port 8000 of the user's machine; accessing the deployed site from another device (localhost refers to the visitor's machine); backend returns 422/500 on start; reverse proxy at a different path.","commonSituations":"Site deployed to Vercel/GitHub Pages but backend is a local demo server; teammate opens the page on their laptop where nothing listens on 8000; backend moved to /api prefix change or port change.","solutions":["Replace the hardcoded URL with a config-derived base: same-origin '' in production behind one proxy, or import.meta.env.VITE_API_BASE / window.location.origin fallback in dev","Confirm the backend actually serves /api/game/start on 8000 (curl -i -X POST http://localhost:8000/api/game/start)","Enable CORS on the backend for the frontend origin if you keep cross-origin calls","If 422/500, read response body detail and include it in the thrown error"],"exampleFix":"// before\nconst response = await fetch('http://localhost:8000/api/game/start', {...});\n\n// after\nconst API_BASE = window.location.origin.startsWith('http://localhost') ? '' : (window.API_BASE_URL || '');\nconst response = await fetch(`${API_BASE}/api/game/start`, {...});","handlingStrategy":"validation","validationCode":"const API_BASE = import.meta.env?.VITE_API_BASE ?? (location.hostname === 'localhost' ? 'http://localhost:8000' : '');","typeGuard":null,"tryCatchPattern":"try { await startNewGame(); } catch (e) { alert(`启动失败：${e.message}`); }","preventionTips":["Never hardcode localhost URLs — derive base from config/env","Health-check the API on app boot and show setup instructions if unreachable","Add backend CORS for the dev origin"],"tags":["hardcoded-url","fetch","http","deployment"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}