jackwener/OpenCLI · error · CommandExecutionError

HTTP ${code}

Error message

HTTP ${code}

What it means

fetchXueqiuJson maps any non-ok HTTP status other than 401/403 to CommandExecutionError(`HTTP ${code}`). This is the generic branch for server-side failures (4xx/5xx) from xueqiu's API — e.g. 400 bad pid parameter, 404 unknown endpoint, 500/502 server trouble. It is thrown so callers get a clean CliError with an actionable hint instead of an opaque network failure.

Source

Thrown at clis/xueqiu/utils.js:55

    const result = await page.evaluate(`(async () => {
    const res = await fetch(${JSON.stringify(url)}, { credentials: 'include' });
    if (!res.ok) return { __xqErr: res.status };
    try {
      return await res.json();
    } catch {
      return { __xqErr: 'parse' };
    }
  })()`);
    const r = result;
    if (r?.__xqErr !== undefined) {
        const code = r.__xqErr;
        if (code === 401 || code === 403) {
            throw new AuthRequiredError('xueqiu.com', '未登录或登录已过期');
        }
        if (code === 'parse') {
            throw new CommandExecutionError('响应不是有效 JSON', '可能触发了风控,请检查登录状态或稍后重试');
        }
        throw new CommandExecutionError(`HTTP ${code}`, '请检查网络连接或登录状态');
    }
    return result;
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Check network connectivity and retry
  2. Verify the pid / query parameters are valid (get group ids via xueqiu groups)
  3. Wait and retry if the status is 429 or 5xx (server-side or rate limit)
  4. Re-login if the error persists alongside other auth symptoms

Example fix

// before (invalid pid)
const url = `...&pid=${encodeURIComponent('abc')}`; // HTTP 400
// after (validate pid first)
const valid = ['-1','-4','-5','-6','-7','-10','0'];
if (!valid.includes(pid)) throw new Error(`invalid pid: ${pid}`);
const url = `...&pid=${encodeURIComponent(pid)}`;
Defensive patterns

Strategy: retry

Validate before calling

if (!/^-?\d+$/.test(pid)) throw new Error(`invalid pid: ${pid}`);

Type guard

function isCommandExecutionError(e) { return e instanceof CommandExecutionError; }

Try / catch

try {
  const d = await fetchXueqiuJson(page, url);
} catch (e) {
  if (e instanceof CommandExecutionError && /^HTTP \d+/.test(e.message)) {
    const status = Number(e.message.match(/HTTP (\d+)/)[1]);
    if (status === 429 || status >= 500) return retryWithBackoff(() => fetchXueqiuJson(page, url));
  }
  throw e;
}

Prevention

When it happens

Trigger: xueqiu API returns status codes like 400 (invalid pid param), 404, 429 (rate limited), or 5xx; any status that is not 200, 401, or 403 and whose body cannot be parsed as valid JSON data.

Common situations: Passing an invalid pid group id; xueqiu API temporarily down or returning 502; rate limiting (429) after rapid calls; endpoint changed server-side.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/94f8168f705d37f1. Report an issue: GitHub.