jackwener/OpenCLI · error · ArgumentError

bilibili unfollow target must be a valid space.bilibili.com/

Error message

bilibili unfollow target must be a valid space.bilibili.com/<uid> URL

What it means

resolveTargetMid in clis/bilibili/unfollow.js throws this ArgumentError when the unfollow target looks like a space.bilibili.com URL (matched by the prefix regex) but parseSpaceMidUrl fails to extract a numeric uid from it. The library intentionally does not fall back to UID/name resolution once the input appears to be a space URL, because treating a malformed URL as a username would silently unfollow the wrong account. It is a fail-fast input validation error.

Source

Thrown at clis/bilibili/unfollow.js:20

 * 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),
        );
    }
}

cli({
    site: 'bilibili',
    name: 'unfollow',
    access: 'write',

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Ensure the target is a complete URL of the form https://space.bilibili.com/<numeric-uid>, e.g. space.bilibili.com/1234567
  2. If you have the numeric uid directly, pass the plain uid string (or username) instead of a URL so it goes through the resolveUid path
  3. Trim whitespace and remove any trailing path fragments (e.g. /dynamic) beyond the uid
  4. Verify the target variable in your script is not empty or partially interpolated before calling the command

Example fix

// before
await bilibiliUnfollow({ target: 'https://space.bilibili.com/' });
// after
await bilibiliUnfollow({ target: 'https://space.bilibili.com/1234567' });
Defensive patterns

Strategy: validation

Validate before calling

function isValidSpaceUrl(t) {
  return /^https?:\/\/space\.bilibili\.com\/\d+\/?$/i.test(String(t ?? '').trim());
}
if (!isValidSpaceUrl(target) && !/^\d+$/.test(String(target ?? '').trim())) {
  throw new Error(`Target must be a numeric uid or full space.bilibili.com/<uid> URL, got: ${target}`);
}

Type guard

function isSpaceUrlWithUid(v) {
  if (typeof v !== 'string') return false;
  const m = v.trim().match(/^(?:https?:\/\/)?space\.bilibili\.com\/(\d+)\/?$/i);
  return m !== null;
}

Try / catch

try {
  await bilibiliUnfollow({ target });
} catch (err) {
  if (err instanceof ArgumentError && /valid space\.bilibili\.com/.test(err.message)) {
    console.error(`Bad target URL: ${target}. Use https://space.bilibili.com/<uid>`);
  } else throw err;
}

Prevention

When it happens

Trigger: Calling `bilibili unfollow --target <url>` (or invoking mid/resolveTargetMid programmatically) where the target matches /^(?:https?:\/\/)?space\.bilibili\.com\//i but the remainder is not a valid numeric uid, e.g. 'space.bilibili.com/', 'space.bilibili.com/abc', 'https://space.bilibili.com' with no path.

Common situations: Copy-pasting a truncated space URL from the browser address bar; hand-typing the URL and forgetting the uid; a script interpolating an empty or non-numeric variable into the URL; pasting a profile link from the bilibili app that uses a different path format.

Related errors


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