maotoumao/MusicFree · error · Error

mediakey不完整

Error message

mediakey不完整

What it means

parseMediaUniqueKey in src/utils/mediaUtils.ts throws Error('mediakey不完整') ("mediakey incomplete") when a media unique key string cannot be split into both platform and id, or an object form lacks either field. Every media item must be identifiable by 'platform@id'; the function refuses ambiguous keys.

Source

Thrown at src/utils/mediaUtils.ts:32

}

/**
 * 解析媒体资源的唯一key
 * @param key 
 * @returns 
 */
export function parseMediaUniqueKey(key: string): ICommon.IMediaBase {
    try {
        const str = JSON.parse(key.trim());
        let platform, id;
        if (typeof str === "string") {
            [platform, id] = str.split("@");
        } else {
            platform = str?.platform;
            id = str?.id;
        }
        if (!platform || !id) {
            throw new Error("mediakey不完整");
        }
        return {
            platform,
            id,
        };
    } catch (e: any) {
        throw e;
    }
}

/**
 * 比较两个媒体资源是否相同
 * @param a 
 * @param b 
 * @returns 
 */
export function isSameMediaItem(
    a: ICommon.IMediaBase | null | undefined,

View on GitHub (pinned to d118b18b3d)

Solutions

  1. Inspect the offending key value (it's in the caught error path) and fix the code that produced the partial platform/id.
  2. Migrate old stored records to the 'platform@id' format during app upgrade/migration.
  3. Guard callers (targetMedia) to skip or repair items whose key fails to parse instead of throwing.
  4. In the plugin, ensure every IMusicItem has both platform and id set before it is stored.

Example fix

// before
const media = targetMedia(someItem);
// after
if (!someItem || !someItem.platform || !someItem.id) {
  console.warn('skip malformed media item', someItem);
  return null;
}
const media = targetMedia(someItem);
Defensive patterns

Strategy: validation

Validate before calling

function isValidMediaKey(key) {
  if (typeof key === 'string') {
    const [p, id] = key.split('@');
    return !!p && !!id;
  }
  return !!key?.platform && !!key?.id;
}
if (!isValidMediaKey(raw)) return null; // skip item

Type guard

function isCompleteMediaKey(k: unknown): k is { platform: string; id: string } {
  return !!k && typeof (k as any).platform === 'string' && typeof (k as any).id === 'string';
}

Try / catch

try {
  const { platform, id } = parseMediaUniqueKey(key);
} catch (e) {
  if (e?.message === 'mediakey不完整') {
    console.warn('skipping malformed media key', key);
    return null;
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling parseMediaUniqueKey (via targetMedia) with a string missing the '@' separator or empty segments (e.g. '@nw003xyz', 'musicfree@'), or an object {platform}/{id} where either is undefined — typically from malformed history/playlist storage entries or hand-built keys.

Common situations: Legacy data written before the '@' key format existed; a plugin returning music items with missing id; deep links / share URLs carrying a truncated key; manual key construction with the wrong delimiter.

Related errors


AI-assisted analysis of maotoumao/MusicFree@d118b18b3d (2026-08-30). Data as JSON: /api/errors/e484f69007740b46. Report an issue: GitHub.