Skip to content

Commit

Permalink
Create load timeout error and guard trigger error (#693)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinHoldstock authored Mar 7, 2018
1 parent 621ab20 commit 8f66050
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const ERROR_CODE = {
CONVERSION_PASSWORD_PROTECTED: 'error_password_protected',
CONVERSION_TRY_AGAIN_LATER: 'error_try_again_later',
CONVERSION_UNSUPPORTED_FORMAT: 'error_unsupported_format',
VIEWER_LOAD_TIMEOUT: 'error_viewer_load_timeout',
CONTENT_DOWNLOAD: 'error_content_download'
};

Expand Down
10 changes: 7 additions & 3 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@ class BaseViewer extends EventEmitter {
this.resetLoadTimeout();
return;
}

if (!this.isLoaded() && !this.isDestroyed()) {
this.triggerError();
const error = new PreviewError(ERROR_CODE.VIEWER_LOAD_TIMEOUT, __('error_refresh'));
this.triggerError(error);
}
}, this.loadTimeout);
}
Expand Down Expand Up @@ -335,14 +337,16 @@ class BaseViewer extends EventEmitter {
*
* @protected
* @emits error
* @param {Error|string} [err] - Optional error or string with message
* @param {Error|PreviewError} [err] - Error object related to the error that happened.
* @return {void}
*/
triggerError(err) {
const message = err ? err.message : '';
const error =
err instanceof PreviewError
? err
: new PreviewError(ERROR_CODE.LOAD_VIEWER, __('error_refresh'), {}, err.message);
: new PreviewError(ERROR_CODE.LOAD_VIEWER, __('error_refresh'), {}, message);

this.emit('error', error);
}

Expand Down
51 changes: 50 additions & 1 deletion src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as dr from '../../downloadReachability';
import * as file from '../../file';
import * as icons from '../../icons/icons';
import * as constants from '../../constants';
import { VIEWER_EVENT, LOAD_METRIC } from '../../events';
import { VIEWER_EVENT, LOAD_METRIC, ERROR_CODE } from '../../events';
import Timer from '../../Timer';

let base;
Expand Down Expand Up @@ -170,6 +170,22 @@ describe('lib/viewers/BaseViewer', () => {
// Test cleanup
clearTimeout(base.loadTimeoutId);
});

it('should trigger an error if the viewer times out', () => {
const triggerStub = sandbox.stub(base, 'triggerError');
sandbox.stub(window, 'setTimeout').callsFake((func) => func());

base.loaded = false;
base.destroyed = false;

base.resetLoadTimeout();
const [ error ] = triggerStub.getCall(0).args;
expect(error).to.be.instanceof(PreviewError);
expect(error.code).to.equal(ERROR_CODE.VIEWER_LOAD_TIMEOUT);

// Test cleanup
clearTimeout(base.loadTimeoutId);
});
});

describe('startLoadTimer()', () => {
Expand Down Expand Up @@ -250,6 +266,39 @@ describe('lib/viewers/BaseViewer', () => {
expect(error.code).to.equal('error_load_viewer');
expect(error.message).to.equal('blah');
});

it('should emit a load viewer error if no error provided', () => {
const stub = sandbox.stub(base, 'emit');
base.triggerError();

expect(base.emit).to.be.called;
const [ event, error ] = stub.getCall(0).args;
expect(event).to.equal('error');
expect(error).to.be.instanceof(PreviewError);
expect(error.code).to.equal('error_load_viewer');

});

it('should pass through the error if it is a PreviewError', () => {
const code = 'my_special_error';
const displayMessage = 'Such a special error!';
const message = 'Bad things have happened';
const details = {
what: 'what?!'
};
const err = new PreviewError(code, displayMessage, details, message);
const stub = sandbox.stub(base, 'emit');
base.triggerError(err);

expect(base.emit).to.be.called;
const [ event, error ] = stub.getCall(0).args;
expect(event).to.equal('error');
expect(error).to.be.instanceof(PreviewError);
expect(error.code).to.equal(code);
expect(error.displayMessage).to.equal(displayMessage);
expect(error.details).to.equal(details);
expect(error.message).to.equal(message);
});
});

describe('isLoaded()', () => {
Expand Down

0 comments on commit 8f66050

Please sign in to comment.