jackwener/OpenCLI · error · CommandExecutionError

${label} returned a malformed flag

Error message

${label} returned a malformed flag

What it means

readOptionalFlag coerces an optional boolean-ish flag from an API payload. If the value is neither null/undefined, boolean, nor number, it throws this CommandExecutionError instead of guessing — protecting the rights/rightsPay/rightsUgcPay/rightsArcPay/upowerExclusive/payPreview outputs from garbage values.

Source

Thrown at clis/bilibili/video.js:23

function requireObject(value, label) {
  if (!value || typeof value !== 'object' || Array.isArray(value)) {
    throw new CommandExecutionError(`${label} returned a malformed payload`);
  }
  return value;
}

function unwrapBrowserResult(value) {
  if (value && typeof value === 'object' && typeof value.session === 'string' && Object.hasOwn(value, 'data')) {
    return value.data;
  }
  return value;
}

function readOptionalFlag(value, label) {
  if (value == null) return false;
  if (typeof value === 'boolean') return value;
  if (typeof value === 'number') return value !== 0;
  throw new CommandExecutionError(`${label} returned a malformed flag`);
}

function readOptionalString(value, label) {
  if (value == null) return '';
  if (typeof value === 'string') return value;
  throw new CommandExecutionError(`${label} returned a malformed string`);
}

cli({
  site: 'bilibili',
  name: 'video',
    access: 'read',
  description: 'Get Bilibili video metadata (title, author, duration, stats, etc.)',
  strategy: Strategy.COOKIE,
  args: [
    { name: 'bvid', required: true, positional: true, help: 'BV ID, video URL, or b23.tv short link' },
    { name: 'page', required: false, help: '分P 选集序号(从 1 开始)。多 P 视频指定某一集,title/cid 返回该集;缺省取整集默认(P1)' },
  ],

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Inspect the raw API response for the affected rights field to confirm its actual type
  2. Loosen the parser to coerce numeric strings (Number(value)) before validation
  3. Catch the error in the command handler and report the field as unknown rather than failing the whole command
  4. Report/patch the schema change in clis/bilibili/video.js

Example fix

// before
if (typeof value === 'number') return value !== 0;
throw new CommandExecutionError(`${label} returned a malformed flag`);
// after
if (typeof value === 'number') return value !== 0;
if (typeof value === 'string' && value.trim() !== '' && !Number.isNaN(Number(value))) return Number(value) !== 0;
throw new CommandExecutionError(`${label} returned a malformed flag`);
Defensive patterns

Strategy: type-guard

Validate before calling

// pre-normalize numeric-string flags before passing on
const normalized = (v) => v == null || typeof v === 'boolean' || typeof v === 'number' ? v : Number(v);

Type guard

function isFlagLike(v) {
  return v == null || typeof v === 'boolean' || (typeof v === 'number' && Number.isFinite(v));
}

Try / catch

try { const rights = await getRights(bvid); }
catch (e) {
  if (e instanceof CommandExecutionError && e.message.includes('malformed flag')) console.warn('Rights field had unexpected type; treating as unknown');
  else throw e;
}

Prevention

When it happens

Trigger: A Bilibili video rights field (e.g. pay, ugc_pay, arc_pay, upower_exclusive, pay_preview) arrives as a string like "0"/"true" or an object instead of a number/boolean — a schema drift or unexpected field type in the API response.

Common situations: Bilibili changes a rights field from numeric to string; a new experimental field type appears in the payload; scraping/middleware converts numbers to strings.

Understand the failure class

Related errors


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