yikart/AiToEarn · error · Error

解析响应数据失败: ${err instanceof Error ? err.message : String(err)

Error message

解析响应数据失败: ${err instanceof Error ? err.message : String(err)}

What it means

If responseText is non-empty but JSON.parse throws (malformed JSON, HTML error page, truncated body), the inner catch logs the raw data and rethrows as '解析响应数据失败: <original message>' (project/aitoearn-electron/electron/plat/douyin/index.ts:2369). The outer catch may retry it; on the final attempt the message is additionally wrapped as '请求失败: 解析响应数据失败: ...'. It indicates Douyin (or an intermediary) returned a non-JSON body.

Source

Thrown at project/aitoearn-electron/electron/plat/douyin/index.ts:2369

          throw new Error(
            '服务器多次返回空数据,请检查网络连接或抖音服务器状态',
          );
        }

        try {
          const result = JSON.parse(responseText);

          if (!result) {
            console.error(`解析后的数据为空`);
            throw new Error('解析后的数据为空');
          }

          return result;
        } catch (err) {
          console.error(`解析响应数据失败:`, err);
          console.error(`导致错误的原始数据:`, responseText);
          throw new Error(
            `解析响应数据失败: ${err instanceof Error ? err.message : String(err)}`,
          );
        }
      } catch (error) {
        if (retryCount < maxRetries - 1) {
          retryCount++;
          console.log(
            `请求失败,${retryDelay / 1000}秒后进行第${retryCount}次重试...`,
          );
          await new Promise((resolve) => setTimeout(resolve, retryDelay));
          continue;
        }

        console.error(`请求发生错误:`, error);
        throw new Error(
          `请求失败: ${error instanceof Error ? error.message : String(error)}`,
        );
      }

View on GitHub (pinned to d3aa8bea5b)

Solutions

  1. Inspect the console.error output '导致错误的原始数据:' to see the raw body — if it is HTML, refresh the Douyin cookie or complete verification (captcha/QR login).
  2. Refresh the Douyin session cookie and re-run the operation.
  3. Check whether the request is going through a proxy/captive portal that injects HTML, and bypass it.
  4. Retry with backoff for transient truncation; verify the endpoint URL is still current after Douyin API updates.

Example fix

// before
const res = await douyinService.somePostFormDataOp(cookie, data);
// after
try {
  const res = await douyinService.somePostFormDataOp(cookie, data);
} catch (e) {
  if (String(e.message).startsWith('解析响应数据失败')) {
    // raw body was logged; refresh cookie or handle HTML/captcha page
  }
  throw e;
}
Defensive patterns

Strategy: try-catch

Type guard

function isJsonObject(text: string): boolean {
  try {
    const v = JSON.parse(text);
    return typeof v === 'object' && v !== null;
  } catch {
    return false;
  }
}

Try / catch

try {
  const result = await douyinService.someOp(cookie, data);
} catch (e) {
  const msg = String(e.message);
  if (msg.startsWith('解析响应数据失败') || msg.includes('解析响应数据失败')) {
    // raw body was logged via console.error; if HTML → refresh cookie / handle captcha
    await refreshDouyinCookie();
  } else throw e;
}

Prevention

When it happens

Trigger: Any postFormData call where Douyin responds with HTML (e.g. a login/captcha/risk-control page), a truncated body, or garbled encoding instead of JSON — the parse throws SyntaxError which is rethrown with this prefix.

Common situations: Expired cookie causing Douyin to serve an HTML login/verification page instead of the JSON API response; a captive portal or proxy injecting HTML; CDN/server returning a compressed or corrupted body; endpoint URL changes after a Douyin API version bump.

Understand the failure class

Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.

Related errors


AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31). Data as JSON: /api/errors/f4b1321ff9491ff1. Report an issue: GitHub.