Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an error setter on the base tech #2517

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,8 @@ class Player extends Component {
* @event error
*/
handleTechError() {
this.error(this.tech.error().code);
let error = this.tech.error();
this.error(error && error.code);
}

/**
Expand Down
13 changes: 6 additions & 7 deletions src/js/tech/flash.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class Flash extends Tech {
// Create setters and getters for attributes
const _api = Flash.prototype;
const _readWrite = 'rtmpConnection,rtmpStream,preload,defaultPlaybackRate,playbackRate,autoplay,loop,mediaGroup,controller,controls,volume,muted,defaultMuted'.split(',');
const _readOnly = 'error,networkState,readyState,initialTime,duration,startOffsetTime,paused,ended,videoTracks,audioTracks,videoWidth,videoHeight'.split(',');
const _readOnly = 'networkState,readyState,initialTime,duration,startOffsetTime,paused,ended,videoTracks,audioTracks,videoWidth,videoHeight'.split(',');

function _createSetter(attr){
var attrUpper = attr.charAt(0).toUpperCase() + attr.slice(1);
Expand Down Expand Up @@ -437,15 +437,14 @@ Flash.onEvent = function(swfID, eventName){
// Log errors from the swf
Flash.onError = function(swfID, err){
const tech = Dom.getEl(swfID).tech;
const msg = 'FLASH: '+err;

// trigger MEDIA_ERR_SRC_NOT_SUPPORTED
if (err === 'srcnotfound') {
tech.trigger('error', { code: 4, message: msg });

// errors we haven't categorized into the media errors
} else {
tech.trigger('error', msg);
return tech.error(4);
}

// trigger a custom error
tech.error('FLASH: ' + err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible change

let error = 'FLASH: ' + err;

if (err === 'srcnotfound') {
  error = 4;
}
tech.error(error);

};

// Flash Version Check
Expand Down
22 changes: 22 additions & 0 deletions src/js/tech/tech.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as Fn from '../utils/fn.js';
import log from '../utils/log.js';
import { createTimeRange } from '../utils/time-ranges.js';
import { bufferedPercent } from '../utils/buffer.js';
import MediaError from '../media-error.js';
import window from 'global/window';
import document from 'global/document';

Expand Down Expand Up @@ -265,6 +266,27 @@ class Tech extends Component {
super.dispose();
}

/**
* When invoked without an argument, returns a MediaError object
* representing the current error state of the player or null if
* there is no error. When invoked with an argument, set the current
* error state of the player.
* @param {MediaError=} err Optional an error object
* @return {MediaError} the current error object or null
* @method error
*/
error(err) {
if (err !== undefined) {
if (err instanceof MediaError) {
this.error_ = err;
} else {
this.error_ = new MediaError(err);
}
this.trigger('error');
}
return this.error_;
}

/**
* Return the time ranges that have been played through for the
* current source. This implementation is incomplete. It does not
Expand Down
19 changes: 19 additions & 0 deletions test/unit/tech/tech.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var noop = function() {}, clock, oldTextTracks;
import Tech from '../../../src/js/tech/tech.js';
import { createTimeRange } from '../../../src/js/utils/time-ranges.js';
import extendsFn from '../../../src/js/extends.js';
import MediaError from '../../../src/js/media-error.js';

q.module('Media Tech', {
'setup': function() {
Expand Down Expand Up @@ -195,6 +196,24 @@ test('should handle unsupported sources with the source handler API', function()
ok(usedNative, 'native source handler was used when an unsupported source was set');
});

test('should allow custom error events to be set', function() {
let tech = new Tech();
let errors = [];
tech.on('error', function() {
errors.push(tech.error());
});

equal(tech.error(), null, 'error is null by default');

tech.error(new MediaError(1));
equal(errors.length, 1, 'triggered an error event');
equal(errors[0].code, 1, 'set the proper code');

tech.error(2);
equal(errors.length, 2, 'triggered an error event');
equal(errors[1].code, 2, 'wrapped the error code');
});

test('should track whether a video has played', function() {
let tech = new Tech();

Expand Down