{"record":{"id":"252433f5a7445d6c","repo":"santifer/career-ops","slug":"wttj-api-env-payload-has-no-json-object","errorCode":null,"errorMessage":"wttj: /api/env payload has no JSON object","messagePattern":"wttj: /api/env payload has no JSON object","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"providers/wttj.mjs","lineNumber":81,"sourceCode":"    throw new Error(`wttj: invalid URL: ${url}`);\n  }\n  if (parsed.protocol !== 'https:') throw new Error(`wttj: URL must use HTTPS: ${url}`);\n  if (parsed.hostname !== host.toLowerCase()) {\n    throw new Error(`wttj: untrusted ${label} hostname \"${parsed.hostname}\" — must be ${host}`);\n  }\n  return url;\n}\n\n/**\n * Parse the `window.env = {...}` payload served by /api/env and extract the\n * Algolia application id + client search key.\n * @param {string} text\n * @returns {{ appId: string, apiKey: string }}\n */\nexport function parseEnvPayload(text) {\n  const start = text.indexOf('{');\n  const end = text.lastIndexOf('}');\n  if (start === -1 || end <= start) throw new Error('wttj: /api/env payload has no JSON object');\n  let env;\n  try {\n    env = JSON.parse(text.slice(start, end + 1));\n  } catch {\n    throw new Error('wttj: /api/env payload is not valid JSON');\n  }\n  const appId = typeof env.PUBLIC_ALGOLIA_APPLICATION_ID === 'string' ? env.PUBLIC_ALGOLIA_APPLICATION_ID.trim() : '';\n  const apiKey = typeof env.PUBLIC_ALGOLIA_API_KEY_CLIENT === 'string' ? env.PUBLIC_ALGOLIA_API_KEY_CLIENT.trim() : '';\n  // App ids are short alphanumerics; validating keeps the derived Algolia\n  // hostname from being attacker-shaped if the env payload ever changes.\n  if (!/^[A-Z0-9]{6,16}$/i.test(appId)) throw new Error(`wttj: unexpected Algolia app id \"${appId}\"`);\n  // The key is only ever sent as a request header (never used to build a\n  // host), so don't over-constrain its format — WTTJ may rotate to a longer\n  // or non-hex (e.g. secured/base64) client key. Length bounds only.\n  if (!apiKey || apiKey.length < 16 || apiKey.length > 500) {\n    throw new Error('wttj: unexpected Algolia api key shape');\n  }\n  return { appId, apiKey };","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/santifer/career-ops/blob/aac998c7ed7248ea853b720ceeb1fdbeb322fc5d/providers/wttj.mjs#L63-L99","documentation":"parseEnvPayload extracts the window.env JSON object from the /api/env response text by slicing from the first '{' to the last '}'. If no braces exist (or they are misordered), it throws, meaning the fetched text does not contain any JSON object at all.","triggerScenarios":"parseEnvPayload called with an empty string, an HTML error/login page, or any text without a '{...}' region — i.e. /api/env did not return the expected env script payload.","commonSituations":"WTTJ serving a bot-block/Cloudflare challenge page instead of /api/env; network middleware returning an error page; the /api/env endpoint path changed or returned an empty body.","solutions":["Inspect the fetched text (log it) to see what /api/env actually returned","Retry the fetch — bot-protection pages are often transient","Verify the /api/env endpoint URL is still correct for the WTTJ board","If the payload format changed, update the extraction logic"],"exampleFix":"// before\nconst env = parseEnvPayload(await res.text());\n// after\nconst text = await res.text();\nif (!text.includes('{')) throw new Error(`/api/env returned non-JSON body: ${text.slice(0, 200)}`);\nconst env = parseEnvPayload(text);","handlingStrategy":"try-catch","validationCode":"const text = await res.text();\nif (typeof text !== 'string' || text.indexOf('{') === -1) throw new Error('env payload missing JSON object; got bot page or error page');","typeGuard":"const hasJsonObject = (text) => typeof text === 'string' && text.indexOf('{') !== -1 && text.lastIndexOf('}') > text.indexOf('{');","tryCatchPattern":"let env;\ntry { env = parseEnvPayload(text); } catch (e) { if (e.message.includes('no JSON object')) { console.warn('/api/env returned a non-JSON page (bot block or error page); retry later'); return null; } throw e; }","preventionTips":["Detect bot-protection/error pages before parsing (check content-type or status)","Retry transient failures with backoff","Snapshot the /api/env response when debugging so format changes are visible"],"tags":["json","payload-parsing","wttj","api-response"],"backgroundTag":"unexpected-response-shape","analyzedSha":"aac998c7ed7248ea853b720ceeb1fdbeb322fc5d","analyzedAt":"2026-09-16T06:35:29.214Z","contentChangedAt":"2026-09-16T06:35:29.214Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}