jackwener/OpenCLI · error · ArgumentError

bilibili unfollow target cannot be empty

Error message

bilibili unfollow target cannot be empty

What it means

resolveTargetMid validates the raw unfollow target before resolving it to a user mid. An empty/blank input (null, undefined, whitespace) throws this ArgumentError immediately, since there is nothing to resolve into a space.bilibili.com mid.

Source

Thrown at clis/bilibili/unfollow.js:15

/**
 * Bilibili unfollow — removes a follow relation via the official write API.
 * Mirror of follow.js with act=2. If the viewer is not currently following the
 * target, the API call is skipped and `not-following` is returned without
 * touching state.
 */
import { cli, Strategy } from '@jackwener/opencli/registry';
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import { parseSpaceMidUrl, fetchRelationAttribute, waitForRelation } from './relation.js';
import { apiPost, getSelfUid, requireOkPayload, resolveUid } from './utils.js';

async function resolveTargetMid(page, raw) {
    const trimmed = String(raw ?? '').trim();
    if (!trimmed) {
        throw new ArgumentError('bilibili unfollow target cannot be empty');
    }
    if (/^(?:https?:\/\/)?space\.bilibili\.com\//i.test(trimmed)) {
        const mid = parseSpaceMidUrl(trimmed);
        if (!mid) {
            throw new ArgumentError('bilibili unfollow target must be a valid space.bilibili.com/<uid> URL');
        }
        return mid;
    }
    try {
        return await resolveUid(page, trimmed);
    } catch (error) {
        if (error instanceof EmptyResultError) throw error;
        throw new ArgumentError(
            `Cannot resolve Bilibili target from input: ${trimmed}`,
            error instanceof Error ? error.message : String(error),
        );
    }
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a non-empty target: a numeric UID or a space.bilibili.com/<uid> URL.
  2. Quote the argument in shell scripts: bilibili unfollow "$TARGET".
  3. Add a required check on the argument before invoking the command.
  4. Echo the variable in scripts to confirm it is populated before the call.
  5. If wrapping the API, validate input with a trim/length check first.

Example fix

// before
const target = process.env.BILI_TARGET;
await run('bilibili', 'unfollow', target); // may be undefined
// after
const target = process.env.BILI_TARGET;
if (!target || !target.trim()) throw new Error('BILI_TARGET is required');
await run('bilibili', 'unfollow', target.trim());
Defensive patterns

Strategy: validation

Validate before calling

const trimmed = String(rawTarget ?? '').trim();
if (!trimmed) throw new Error('unfollow target (UID or space.bilibili.com URL) is required');

Type guard

function isValidUnfollowTarget(raw) {
  const t = String(raw ?? '').trim();
  return t.length > 0 && (/^\d+$/.test(t) || /^(?:https?:\/\/)?space\.bilibili\.com\//i.test(t));
}

Try / catch

try {
  await unfollow(page, target);
} catch (e) {
  if (String(e.message).includes('target cannot be empty')) {
    console.error('Usage: bilibili unfollow <uid | space.bilibili.com/<uid> URL>');
    process.exitCode = 2;
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `bilibili unfollow` without the positional target argument, passing an empty string, or programmatically invoking resolveTargetMid/mid with an unset variable.

Common situations: Shell scripts where the target variable is unquoted or unset; missing required-arg validation upstream; copy-paste commands with the UID omitted.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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