{"record":{"id":"06593a4d75670e28","repo":"jackwener/OpenCLI","slug":"chess-com-callback-returned-an-invalid-response-ob","errorCode":null,"errorMessage":"Chess.com callback returned an invalid response object for ${url}","messagePattern":"Chess\\.com callback returned an invalid response object for (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/chess/game.js","lineNumber":96,"sourceCode":"        { name: 'game-url', type: 'string', required: true, positional: true, help: 'Full game URL, e.g. https://www.chess.com/game/live/168842570216' },\n    ],\n    columns: [\n        'kind', 'game_id', 'date',\n        'white', 'white_rating', 'black', 'black_rating',\n        'result', 'winner_color', 'termination',\n        'eco', 'time_control', 'rated', 'ply_count', 'url',\n    ],\n    func: async (kwargs) => {\n        const { kind, id } = parseGameUrl(kwargs['game-url']);\n        const url = `${CALLBACK_BASE}/${kind}/game/${id}`;\n        let resp;\n        try {\n            resp = await fetch(url, { headers: { 'User-Agent': UA, accept: 'application/json' } });\n        } catch (error) {\n            throw new CommandExecutionError(`Failed to fetch Chess.com callback ${url}: ${error?.message || error}`);\n        }\n        if (!resp || typeof resp !== 'object') {\n            throw new CommandExecutionError(`Chess.com callback returned an invalid response object for ${url}`);\n        }\n        if (resp.status === 404) {\n            throw new EmptyResultError(`Chess.com has no ${kind} game with id ${id}`);\n        }\n        if (!resp.ok) {\n            throw new CommandExecutionError(`Chess.com callback returned HTTP ${resp.status} for ${url}`);\n        }\n        let payload;\n        try {\n            payload = await resp.json();\n        } catch (error) {\n            throw new CommandExecutionError(`Chess.com callback returned malformed JSON for ${url}: ${error?.message || error}`);\n        }\n        return [summarizeGame({ kind, id, payload })];\n    },\n});\n\nexport const __test__ = { parseGameUrl, summarizeGame };","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/chess/game.js#L78-L114","documentation":"This library fetches a single Chess.com game via the internal callback endpoint /callback/{kind}/game/{id}. After the fetch resolves, it expects a valid Response-like object; if fetch resolved with null, undefined, or a non-object, the command throws CommandExecutionError. This is a defensive invariant check against a misbehaving fetch implementation or a mocked/patched global fetch.","triggerScenarios":"fetch() at clis/chess/game.js:91 resolves to a falsy value or a non-object (e.g. a test stub returning undefined, a proxy/patched fetch returning null, or a non-standard runtime whose fetch resolves non-Response values).","commonSituations":"Running under a test harness with an incomplete fetch mock; custom Node environments with a polyfilled fetch that violates the spec; code interception (e.g. an HTTP mocking lib misconfigured to return undefined for unmatched routes).","solutions":["Check that any global fetch mock/polyfill returns a Response-like object (at minimum {status, ok, json()}) for every matched URL","Fix unmatched-route behavior in HTTP mocking libraries (e.g. nock/msw) so unmatched requests return a Response instead of undefined","Run in a runtime with spec-compliant fetch (Node 18+ or undici) instead of a custom polyfill"],"exampleFix":"// before (broken mock)\nglobal.fetch = async () => undefined;\n// after\nconst resp = new Response(JSON.stringify({ game: { pgnHeaders: { White: 'a', Black: 'b', Result: '1-0' } } }), { status: 200, headers: { 'content-type': 'application/json' } });\nglobal.fetch = async () => resp;","handlingStrategy":"type-guard","validationCode":"// before calling the command, ensure your fetch environment is spec-compliant\nif (typeof globalThis.fetch !== 'function') throw new Error('fetch unavailable');","typeGuard":"function isResponseLike(r) { return !!r && typeof r === 'object' && typeof r.status === 'number' && typeof r.ok === 'boolean' && typeof r.json === 'function'; }","tryCatchPattern":"try {\n  const rows = await chessGameCmd(url);\n} catch (e) {\n  if (/invalid response object/.test(e.message)) {\n    // fix or replace the fetch polyfill/mock, then retry\n  } else throw e;\n}","preventionTips":["Never stub fetch with functions returning undefined/null; always return a Response object","Test against real fetch (Node 18+) in CI","Pin mocking libs (nock/msw) and configure a fallback for unmatched routes"],"tags":["network","fetch","http-client","invariant"],"backgroundTag":"invalid-fetch-response","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}