{"record":{"id":"e484f69007740b46","repo":"maotoumao/MusicFree","slug":"mediakey","errorCode":null,"errorMessage":"mediakey不完整","messagePattern":"mediakey不完整","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/utils/mediaUtils.ts","lineNumber":32,"sourceCode":"}\n\n/**\n * 解析媒体资源的唯一key\n * @param key \n * @returns \n */\nexport function parseMediaUniqueKey(key: string): ICommon.IMediaBase {\n    try {\n        const str = JSON.parse(key.trim());\n        let platform, id;\n        if (typeof str === \"string\") {\n            [platform, id] = str.split(\"@\");\n        } else {\n            platform = str?.platform;\n            id = str?.id;\n        }\n        if (!platform || !id) {\n            throw new Error(\"mediakey不完整\");\n        }\n        return {\n            platform,\n            id,\n        };\n    } catch (e: any) {\n        throw e;\n    }\n}\n\n/**\n * 比较两个媒体资源是否相同\n * @param a \n * @param b \n * @returns \n */\nexport function isSameMediaItem(\n    a: ICommon.IMediaBase | null | undefined,","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/maotoumao/MusicFree/blob/d118b18b3d0c904400f7eea7bf99c0ceec6c1aee/src/utils/mediaUtils.ts#L14-L50","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the offending key value (it's in the caught error path) and fix the code that produced the partial platform/id.","Migrate old stored records to the 'platform@id' format during app upgrade/migration.","Guard callers (targetMedia) to skip or repair items whose key fails to parse instead of throwing.","In the plugin, ensure every IMusicItem has both platform and id set before it is stored."],"exampleFix":"// before\nconst media = targetMedia(someItem);\n// after\nif (!someItem || !someItem.platform || !someItem.id) {\n  console.warn('skip malformed media item', someItem);\n  return null;\n}\nconst media = targetMedia(someItem);","handlingStrategy":"validation","validationCode":"function isValidMediaKey(key) {\n  if (typeof key === 'string') {\n    const [p, id] = key.split('@');\n    return !!p && !!id;\n  }\n  return !!key?.platform && !!key?.id;\n}\nif (!isValidMediaKey(raw)) return null; // skip item","typeGuard":"function isCompleteMediaKey(k: unknown): k is { platform: string; id: string } {\n  return !!k && typeof (k as any).platform === 'string' && typeof (k as any).id === 'string';\n}","tryCatchPattern":"try {\n  const { platform, id } = parseMediaUniqueKey(key);\n} catch (e) {\n  if (e?.message === 'mediakey不完整') {\n    console.warn('skipping malformed media key', key);\n    return null;\n  }\n  throw e;\n}","preventionTips":["Validate plugin-returned IMusicItem objects (platform + id present) before storing.","Run data migrations when the key format changes.","Filter playlist/history records that fail key parsing instead of crashing on one bad record.","Always build keys with a shared util rather than string concatenation."],"tags":["media-key","parsing","data-integrity","plugin"],"backgroundTag":"malformed-media-unique-key","analyzedSha":"d118b18b3d0c904400f7eea7bf99c0ceec6c1aee","analyzedAt":"2026-08-30T11:49:12.932Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}