jackwener/OpenCLI · error

API failed

Error message

API failed

What it means

Inside the browser-evaluated script of the nowcoder practice command, the getPCIntelligentList response is checked with if (!d.success) throw new Error(d.msg || 'API failed'). So 'API failed' is the in-page fallback message shown when Nowcoder's intelligent-recommendation API returns success:false and supplies no msg. The error is thrown inside the page context and then surfaces as the command's failure.

Source

Thrown at clis/nowcoder/practice.js:21

cli({
    site: 'nowcoder',
    name: 'practice',
    access: 'read',
    description: 'Categorized practice questions with progress',
    domain: 'www.nowcoder.com',
    args: [
        { name: 'job', type: 'str', default: '11226', help: 'Career ID (11226=Software, 11227=Hardware, 11229=Product, 11230=Finance)' },
        { name: 'limit', type: 'int', default: 20, help: 'Number of items' },
    ],
    columns: ['category', 'subject', 'total', 'done', 'remaining'],
    pipeline: [
        { navigate: 'https://www.nowcoder.com' },
        { evaluate: `(async () => {
  const jobId = \${{ args.job | json }};
  const limit = \${{ args.limit }};
  const r = await fetch('https://gw-c.nowcoder.com/api/sparta/intelligent/getPCIntelligentList?jobId=' + jobId, {credentials: 'include'});
  const d = await r.json();
  if (!d.success) throw new Error(d.msg || 'API failed');
  const all = [];
  for (const tag of (d.data?.tags || [])) {
    for (const item of (tag.items || [])) {
      all.push({
        category: tag.title || 'recommended',
        subject: item.title,
        total: item.tcount,
        done: item.rcount,
        remaining: item.leftCount,
      });
    }
  }
  return all.slice(0, limit);
})()
` },
    ],
});

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Re-run with a valid jobId (check the job list on nowcoder.com)
  2. Log in to nowcoder.com in the automated browser so the include-credentials fetch carries a session
  3. Retry later — the endpoint may be rate limiting
  4. Inspect the current API response and update the endpoint/params in clis/nowcoder/practice.js if the schema changed

Example fix

// before
const r = await fetch('...getPCIntelligentList?jobId=' + jobId, {credentials:'include'});
// after
const r = await fetch('...getPCIntelligentList?jobId=' + jobId, {credentials:'include'});
if (!r.ok) throw new Error('HTTP ' + r.status); // distinguish transport vs business errors
const d = await r.json();
Defensive patterns

Strategy: retry

Validate before calling

// validate args before running the command
if (!Number.isSafeInteger(jobId) || jobId <= 0) throw new Error('valid jobId required');

Type guard

function hasSuccess(d){ return !!d && typeof d === 'object' && d.success === true; }

Try / catch

try { await run(['nowcoder', 'practice', '--job', String(jobId)]); }
catch (e) {
  if (String(e.message).includes('API failed')) { await sleep(3000); retryOnce(); } // often transient/session
  else throw e;
}

Prevention

When it happens

Trigger: Running `nowcoder practice` when gw-c.nowcoder.com/api/sparta/intelligent/getPCIntelligentList responds with success:false (bad jobId, not logged in with credentials:'include', throttled, or API decommissioned), and d.msg is empty.

Common situations: An invalid or stale jobId argument; an expired Nowcoder session so the personalized recommendation endpoint refuses; Nowcoder renaming/retiring the sparta intelligent API.

Related errors


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