Skip to content

Commit ed30446

Browse files
authored
Trigger ATTACH_MEDIA_ERROR when calling attachMedia with falsey media argument (#6556)
Resolves #6463
1 parent cbe1461 commit ed30446

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

api-extractor/report/hls.js.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,8 @@ export interface ErrorData {
10541054
//
10551055
// @public (undocumented)
10561056
export enum ErrorDetails {
1057+
// (undocumented)
1058+
ATTACH_MEDIA_ERROR = "attachMediaError",
10571059
// (undocumented)
10581060
AUDIO_TRACK_LOAD_ERROR = "audioTrackLoadError",
10591061
// (undocumented)

src/errors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export enum ErrorDetails {
8585
INTERNAL_EXCEPTION = 'internalException',
8686
// Identifier for an internal call to abort a loader
8787
INTERNAL_ABORTED = 'aborted',
88+
// Triggered when attachMedia fails
89+
ATTACH_MEDIA_ERROR = 'attachMediaError',
8890
// Uncategorized error
8991
UNKNOWN = 'unknown',
9092
}

src/hls.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,17 @@ export default class Hls implements HlsEventEmitter {
389389
* Attaches Hls.js to a media element
390390
*/
391391
attachMedia(media: HTMLMediaElement) {
392+
if (!media) {
393+
const error = new Error(`attachMedia failed: media argument is ${media}`);
394+
this.trigger(Events.ERROR, {
395+
type: ErrorTypes.OTHER_ERROR,
396+
details: ErrorDetails.ATTACH_MEDIA_ERROR,
397+
fatal: true,
398+
error,
399+
});
400+
return;
401+
}
402+
392403
this.logger.log('attachMedia');
393404
this._media = media;
394405
this.trigger(Events.MEDIA_ATTACHING, { media: media });

tests/unit/hls.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import Hls from '../../src/hls';
22
import { hlsDefaultConfig } from '../../src/config';
33
import { Events } from '../../src/events';
4+
import { ErrorTypes, ErrorDetails } from '../../src/errors';
45

56
import chai from 'chai';
67
import sinonChai from 'sinon-chai';
8+
import sinon from 'sinon';
79

810
chai.use(sinonChai);
911
const expect = chai.expect;
@@ -51,7 +53,8 @@ describe('Hls', function () {
5153
);
5254
});
5355
}
54-
it('should add and remove refrences to the "media" element immediately', function () {
56+
57+
it('should add and remove references to the "media" element immediately', function () {
5558
const hls = new Hls({ capLevelOnFPSDrop: true });
5659
expect(hls.media).to.equal(null);
5760
const media = document.createElement('video');
@@ -62,7 +65,7 @@ describe('Hls', function () {
6265
hls.destroy();
6366
});
6467

65-
it('should add and remove refrences to the "media" element after attached', function () {
68+
it('should add and remove references to the "media" element after attached', function () {
6669
const hls = new Hls({
6770
capLevelOnFPSDrop: true,
6871
emeEnabled: true,
@@ -77,14 +80,45 @@ describe('Hls', function () {
7780
detachTest(hls, media, 12);
7881
hls.destroy();
7982
});
83+
84+
it('should trigger an error event when attachMedia is called with null', function () {
85+
const hls = new Hls();
86+
const triggerSpy = sinon.spy(hls, 'trigger');
87+
88+
hls.on(Events.ERROR, function (_event, _data) {});
89+
(hls as any).attachMedia(null);
90+
91+
const expectedEvent = {
92+
type: ErrorTypes.OTHER_ERROR,
93+
details: ErrorDetails.ATTACH_MEDIA_ERROR,
94+
fatal: true,
95+
error: sinon.match
96+
.instanceOf(Error)
97+
.and(
98+
sinon.match.has(
99+
'message',
100+
'attachMedia failed: media argument is null',
101+
),
102+
),
103+
};
104+
105+
expect(triggerSpy).to.be.calledWith(
106+
Events.ERROR,
107+
sinon.match(expectedEvent),
108+
);
109+
110+
triggerSpy.restore();
111+
hls.destroy();
112+
});
80113
});
81114

82115
describe('loadSource and url', function () {
83-
it('url should initally be null', function () {
116+
it('url should initially be null', function () {
84117
const hls = new Hls();
85118
expect(hls.url).to.equal(null);
86119
hls.destroy();
87120
});
121+
88122
it('should return given url after load', function () {
89123
const hls = new Hls();
90124
hls.loadSource(
@@ -95,6 +129,7 @@ describe('Hls', function () {
95129
);
96130
hls.destroy();
97131
});
132+
98133
it('should make relative url absolute', function () {
99134
const hls = new Hls();
100135
hls.loadSource('/streams/x36xhzz/x36xhzz.m3u8');

0 commit comments

Comments
 (0)