{"record":{"id":"4c933c6a7b751d19","repo":"bilibili/flv.js","slug":"mediadatasource-must-has-type-field-to-indicate-vi","errorCode":null,"errorMessage":"MediaDataSource must has type field to indicate video file type!","messagePattern":"MediaDataSource must has type field to indicate video file type!","errorType":"validation","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/flv.js","lineNumber":43,"sourceCode":"import {ErrorTypes, ErrorDetails} from './player/player-errors.js';\nimport LoggingControl from './utils/logging-control.js';\nimport {InvalidArgumentException} from './utils/exception.js';\n\n// here are all the interfaces\n\n// install polyfills\nPolyfill.install();\n\n\n// factory method\nfunction createPlayer(mediaDataSource, optionalConfig) {\n    let mds = mediaDataSource;\n    if (mds == null || typeof mds !== 'object') {\n        throw new InvalidArgumentException('MediaDataSource must be an javascript object!');\n    }\n\n    if (!mds.hasOwnProperty('type')) {\n        throw new InvalidArgumentException('MediaDataSource must has type field to indicate video file type!');\n    }\n\n    switch (mds.type) {\n        case 'flv':\n            return new FlvPlayer(mds, optionalConfig);\n        default:\n            return new NativePlayer(mds, optionalConfig);\n    }\n}\n\n\n// feature detection\nfunction isSupported() {\n    return Features.supportMSEH264Playback();\n}\n\nfunction getFeatureList() {\n    return Features.getFeatureList();","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/bilibili/flv.js/blob/42343088f22619cf014a057b3878fe3d320016a6/src/flv.js#L25-L61","documentation":"createPlayer() also requires the mediaDataSource object to have a 'type' field that selects the player implementation (e.g. 'flv'). The object exists but is missing the discriminator, so the factory cannot decide which player to construct and throws InvalidArgumentException. The switch on mds.type (with 'flv' handled) sits directly after this check.","triggerScenarios":"Passing {url:'...'} or {isLive:true, url:'...'} without type; misspelling the field (e.g. Type or filetype); building the config dynamically and dropping the key; passing a config object from an older schema version.","commonSituations":"Hand-written config objects missing the discriminator; server-supplied configs that omit type; upgrading the library where a previously defaulted field is now mandatory; destructuring/rebuilding the config and forgetting type.","solutions":["Add type:'flv' (or the supported type) to the mediaDataSource object","Check the field spelling is exactly lowercase 'type'","Validate the config shape before calling createPlayer"],"exampleFix":"// before\ncreatePlayer({isLive: true, url: 'https://example.com/live.flv'}, config); // throws\n// after\ncreatePlayer({type: 'flv', isLive: true, url: 'https://example.com/live.flv'}, config);","handlingStrategy":"validation","validationCode":"if (typeof mediaDataSource === 'object' && mediaDataSource !== null && !Object.prototype.hasOwnProperty.call(mediaDataSource, 'type')) {\n  throw new TypeError('mediaDataSource must include a \"type\" field (e.g. \"flv\")');\n}","typeGuard":"function hasPlayerType(v) {\n  return typeof v === 'object' && v !== null && 'type' in v && typeof v.type === 'string' && v.type.length > 0;\n}","tryCatchPattern":"try {\n  const player = createPlayer(mediaDataSource, config);\n} catch (e) {\n  if (e.name === 'InvalidArgumentException') {\n    console.error('mediaDataSource missing \"type\" field:', mediaDataSource);\n  } else { throw e; }\n}","preventionTips":["Always include type:'flv' in the mediaDataSource object","Validate config objects from servers/JSON against a schema before use","Use TypeScript types or JSDoc to enforce the type field","Check for exact lowercase spelling 'type' when configs are hand-written"],"tags":["validation","configuration","missing-field","factory"],"backgroundTag":"missing-required-argument","analyzedSha":"42343088f22619cf014a057b3878fe3d320016a6","analyzedAt":"2026-09-01T00:27:28.147Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}