jackwener/OpenCLI · error · CommandExecutionError

Chess.com stats payload for ${kind}.best is not an object

Error message

Chess.com stats payload for ${kind}.best is not an object

What it means

summarizeStats() validates that k.best (best rating snapshot) is either absent/null or a plain object. This throw fires when k.best is present but some other type (string, number, array). It guards the later k.best?.rating lookup so a corrupt field cannot silently yield wrong output.

Source

Thrown at clis/chess/utils.js:79

    }
    if (!isPlainObject(payload)) {
        throw new CommandExecutionError(`Chess.com API returned an unexpected payload shape for ${url}`);
    }
    return payload;
}

/** Pull rating + record fields out of a stats sub-object (`chess_rapid` etc). */
export function summarizeStats(stats, kind) {
    const k = stats?.[kind];
    if (!k) return null;
    if (!isPlainObject(k)) {
        throw new CommandExecutionError(`Chess.com stats payload for ${kind} is not an object`);
    }
    if (!isOptionalPlainObject(k.last)) {
        throw new CommandExecutionError(`Chess.com stats payload for ${kind}.last is not an object`);
    }
    if (!isOptionalPlainObject(k.best)) {
        throw new CommandExecutionError(`Chess.com stats payload for ${kind}.best is not an object`);
    }
    if (!isOptionalPlainObject(k.record)) {
        throw new CommandExecutionError(`Chess.com stats payload for ${kind}.record is not an object`);
    }
    const record = isPlainObject(k.record) ? k.record : {};
    return {
        kind: kind.replace(/^chess_/, ''),
        rating_current: k.last?.rating ?? '',
        rating_best: k.best?.rating ?? '',
        wins: record.win ?? '',
        losses: record.loss ?? '',
        draws: record.draw ?? '',
    };
}

/** Parse an end_time epoch (seconds) into YYYY-MM-DD. */
export function formatDate(epochSeconds) {
    if (!epochSeconds || typeof epochSeconds !== 'number') return '';

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Re-fetch the stats payload directly from https://api.chess.com/pub/player/{username}/stats to get the documented shape
  2. Find and fix the intermediate transform that converted best from an object into a scalar
  3. In mocks/fixtures use best: { rating: 1620, date: 1700000000 }
  4. If the schema changed upstream, update summarizeStats accordingly

Example fix

// before
stats.chess_rapid.best = 1620;
// after
stats.chess_rapid.best = { rating: 1620, date: Math.floor(Date.now() / 1000) };
Defensive patterns

Strategy: validation

Validate before calling

const best = stats?.[kind]?.best;
if (best != null && (typeof best !== 'object' || Array.isArray(best))) {
  throw new Error(`${kind}.best must be an object like { rating, date }`);
}

Type guard

function hasValidBest(k) {
  return k?.best == null ||
    (typeof k.best === 'object' && !Array.isArray(k.best) && typeof k.best.rating === 'number');
}

Try / catch

try {
  const row = summarizeStats(stats, 'chess_blitz');
} catch (err) {
  if (String(err.message).includes('.best is not an object')) {
    console.warn('best section malformed; ignoring best-rating column for this kind');
  } else { throw err; }
}

Prevention

When it happens

Trigger: stats[kind].best in the payload fetched by rows()/row() is a truthy non-object, e.g. best: 1620 or best: "1620".

Common situations: A normalization step replaced best with the numeric peak rating; stale cache from an older tool version that flattened the field; hand-written mock data; Chess.com schema drift for the stats endpoint.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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