{"record":{"id":"d4eda5ad490ba32e","repo":"jackwener/OpenCLI","slug":"api-failed-d4eda5","errorCode":null,"errorMessage":" API failed: ","messagePattern":" API failed: ","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"clis/tiktok/utils.js","lineNumber":256,"sourceCode":"    throw new Error('HTTP ' + res.status + ' from ' + requestUrl + ': ' + text.slice(0, 160));\n  }\n  if (!text.trim()) return {};\n  try {\n    return JSON.parse(text);\n  } catch (error) {\n    throw new Error('invalid JSON from ' + requestUrl + ': ' + (error instanceof Error ? error.message : String(error)));\n  }\n}\n\nfunction assertTikTokApiSuccess(data, label) {\n  if (!data || typeof data !== 'object') return;\n  const code = data.status_code ?? data.statusCode;\n  if (code === undefined || code === null || Number(code) === 0) return;\n  const message = cleanText(data.status_msg ?? data.statusMsg ?? data.message ?? data.msg ?? code, 240);\n  if (Number(code) === 8 || /auth|captcha|login|permission|unauthori[sz]ed|forbidden/i.test(message)) {\n    throw new Error('AUTH_REQUIRED: ' + label + ' API failed: ' + message);\n  }\n  throw new Error(label + ' API failed: ' + message);\n}\n\nfunction findUniversalData() {\n  const scripts = Array.from(document.querySelectorAll('script'));\n  for (const script of scripts) {\n    const text = script.textContent || '';\n    if (!text || text.length < 32) continue;\n    const trimmed = text.trim();\n    if (!(trimmed.startsWith('{') || trimmed.startsWith('['))) continue;\n    if (\n      !text.includes('webapp.user-detail') &&\n      !text.includes('webapp.recommend-feed') &&\n      !text.includes('webapp.live-discover') &&\n      !text.includes('ItemModule') &&\n      !text.includes('itemList') &&\n      !text.includes('userInfo') &&\n      !text.includes('userList') &&\n      !text.includes('noticeList')","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/tiktok/utils.js#L238-L274","documentation":"The non-auth branch of assertTikTokApiSuccess: a TikTok API payload with a non-zero status_code whose message does not look auth-related is thrown as `<label> API failed: <message>`. The literal ' API failed: ' is the seam between the caller-supplied label and TikTok's status_msg.","triggerScenarios":"TikTok API returned status_code other than 0/8 with a status_msg not matching the auth regex — e.g. content removed, parameter errors, or private/region-locked data.","commonSituations":"Querying a deleted or private account; wrong parameter format for the endpoint; TikTok API returning a business error code (content not exist, forbidden region) that isn't auth-related.","solutions":["Read status_msg after 'API failed: ' — it is TikTok's own error description","Verify the input parameters (user id / video id) still exist and are public","Map the numeric status_code via TikTok API docs to the specific failure","If the message is actually auth-flavored, report/extend the regex so it routes to AUTH_REQUIRED"],"exampleFix":"// before\nassertTikTokApiSuccess(data, 'user/detail');\n// after\ntry {\n  assertTikTokApiSuccess(data, 'user/detail');\n} catch (e) {\n  if (e.message.includes('user/detail API failed')) {\n    console.warn('TikTok rejected request:', e.message);\n  }\n  throw e;\n}","handlingStrategy":"validation","validationCode":"const code = data?.status_code ?? data?.statusCode;\nif (code !== undefined && Number(code) !== 0) console.warn(label, 'reports status', code, data?.status_msg ?? data?.statusMsg);","typeGuard":"function isApiSuccess(data) {\n  if (!data || typeof data !== 'object') return true;\n  const code = data.status_code ?? data.statusCode;\n  return code === undefined || code === null || Number(code) === 0;\n}","tryCatchPattern":"if (!isApiSuccess(data)) {\n  console.warn(`${label} failed:`, data.status_msg ?? data.statusMsg);\n  return null; // skip and continue instead of aborting the batch\n}","preventionTips":["Verify target entities still exist/are public before querying","Check TikTok API docs for the numeric status_code meaning","Skip-and-log non-fatal business errors in batch jobs","Validate inputs (ids, slugs) against expected formats"],"tags":["api","tiktok","business-error"],"backgroundTag":"api-status-code-failure","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}