Skip to content

Commit

Permalink
Fix: Prevent Annotations errors from triggering a Preview failure (#904)
Browse files Browse the repository at this point in the history
- Adds ability to emit silent preview errors without triggering the Preview error viewer. This is useful for cases such as annotations, where failures don't necessarily mean that the entire preview experience needs to fail
  • Loading branch information
pramodsum authored Jan 29, 2019
1 parent 72bf820 commit 0db8b4b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 44 deletions.
9 changes: 2 additions & 7 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1204,12 +1204,6 @@ class Preview extends EventEmitter {
case VIEWER_EVENT.progressEnd:
this.ui.finishProgressBar();
break;
case VIEWER_EVENT.notificationShow:
this.ui.showNotification(data.data);
break;
case VIEWER_EVENT.notificationHide:
this.ui.hideNotification();
break;
case VIEWER_EVENT.mediaEndAutoplay:
this.navigateRight();
break;
Expand Down Expand Up @@ -1450,7 +1444,8 @@ class Preview extends EventEmitter {
this.emitPreviewError(err);

// If preview is closed don't do anything
if (!this.open) {
const isSilent = getProp(err, 'details.silent', false);
if (!this.open || isSilent) {
return;
}

Expand Down
27 changes: 9 additions & 18 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1740,22 +1740,6 @@ describe('lib/Preview', () => {
expect(preview.ui.finishProgressBar).to.be.called;
});

it('should show notification with message on notificationshow event', () => {
const message = 'notification_message';
sandbox.stub(preview.ui, 'showNotification');
preview.handleViewerEvents({
event: VIEWER_EVENT.notificationShow,
data: message
});
expect(preview.ui.showNotification).to.be.calledWith(message);
});

it('should hide notification on notificationhide event', () => {
sandbox.stub(preview.ui, 'hideNotification');
preview.handleViewerEvents({ event: VIEWER_EVENT.notificationHide });
expect(preview.ui.hideNotification).to.be.called;
});

it('should navigate right on mediaendautoplay event', () => {
sandbox.stub(preview, 'navigateRight');
const data = { event: VIEWER_EVENT.mediaEndAutoplay };
Expand Down Expand Up @@ -2176,7 +2160,14 @@ describe('lib/Preview', () => {
it('should only log an error if the preview is closed', () => {
preview.open = false;

preview.triggerError(new Error('fail'));
preview.triggerError(new PreviewError('fail'));
expect(stubs.uncacheFile).to.not.be.called;
expect(stubs.destroy).to.not.be.called;
expect(stubs.emitPreviewError).to.be.called;
});

it('should only log an error if the error is silent', () => {
preview.triggerError(new PreviewError('fail', '', { silent: true }));
expect(stubs.uncacheFile).to.not.be.called;
expect(stubs.destroy).to.not.be.called;
expect(stubs.emitPreviewError).to.be.called;
Expand All @@ -2190,7 +2181,7 @@ describe('lib/Preview', () => {
});

it('should get the error viewer, attach viewer listeners, and load the error viewer', () => {
const err = new Error();
const err = new PreviewError();
preview.triggerError(err);

expect(stubs.getErrorViewer).to.be.called;
Expand Down
11 changes: 6 additions & 5 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,8 @@ class BaseViewer extends EventEmitter {
viewerOptions[this.options.viewer.NAME] = this.viewerConfig;

if (!global.BoxAnnotations) {
const error = new PreviewError(ERROR_CODE.LOAD_ANNOTATIONS);
const error = new PreviewError(ERROR_CODE.LOAD_ANNOTATIONS, __('annotations_load_error'), { silent: true });
this.previewUI.notification.show(error.displayMessage);
this.triggerError(error);
return;
}
Expand Down Expand Up @@ -1009,22 +1010,22 @@ class BaseViewer extends EventEmitter {
this.disableViewerControls();

if (data.data.mode === ANNOTATION_TYPE_POINT) {
this.emit(VIEWER_EVENT.notificationShow, __('notification_annotation_point_mode'));
this.previewUI.notification.show(__('notification_annotation_point_mode'));
} else if (data.data.mode === ANNOTATION_TYPE_DRAW) {
this.emit(VIEWER_EVENT.notificationShow, __('notification_annotation_draw_mode'));
this.previewUI.notification.show(__('notification_annotation_draw_mode'));
this.previewUI.replaceHeader(data.data.headerSelector);
}
break;
case ANNOTATOR_EVENT.modeExit:
this.enableViewerControls();
this.emit(VIEWER_EVENT.notificationHide);
this.previewUI.notification.hide();

if (data.data.mode === ANNOTATION_TYPE_DRAW) {
this.previewUI.replaceHeader(data.data.headerSelector);
}
break;
case ANNOTATOR_EVENT.error:
this.emit(VIEWER_EVENT.notificationShow, data.data);
this.previewUI.notification.show(data.data);
break;
case ANNOTATOR_EVENT.fetch:
this.emit('scale', {
Expand Down
29 changes: 15 additions & 14 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ describe('lib/viewers/BaseViewer', () => {
}
}
});
base.previewUI = {
replaceHeader: sandbox.stub(),
notification: {
show: sandbox.stub(),
hide: sandbox.stub()
}
};
});

afterEach(() => {
Expand Down Expand Up @@ -270,6 +277,7 @@ describe('lib/viewers/BaseViewer', () => {
expect(error).to.be.instanceof(PreviewError);
expect(error.code).to.equal('error_load_viewer');
expect(error.message).to.equal('blah');
expect(base.emit).to.be.calledWith('error', error);
});

it('should emit a load viewer error if no error provided', () => {
Expand Down Expand Up @@ -466,12 +474,6 @@ describe('lib/viewers/BaseViewer', () => {

it('should show the notification if downloads are degraded and we have not shown the notification yet', () => {
stubs.getDownloadNotificationToShow.returns('dl3.boxcloud.com');
base.previewUI = {
notification: {
show: sandbox.stub()
}
};

sandbox.stub(DownloadReachability, 'setDownloadHostNotificationShown');

base.viewerLoadHandler({ scale: 1.5 });
Expand Down Expand Up @@ -1037,6 +1039,8 @@ describe('lib/viewers/BaseViewer', () => {
};

sandbox.stub(base, 'initAnnotations');
sandbox.stub(base, 'emit');
sandbox.stub(base, 'triggerError');
});

it('should not create the annotator if annotations are not enabled', () => {
Expand Down Expand Up @@ -1244,9 +1248,6 @@ describe('lib/viewers/BaseViewer', () => {
};
sandbox.stub(base, 'disableViewerControls');
sandbox.stub(base, 'enableViewerControls');
base.previewUI = {
replaceHeader: () => {}
};
});

it('should disable controls and show point mode notification on annotationmodeenter', () => {
Expand All @@ -1256,7 +1257,7 @@ describe('lib/viewers/BaseViewer', () => {
};
base.handleAnnotatorEvents(data);
expect(base.disableViewerControls).to.be.called;
expect(base.emit).to.be.calledWith(VIEWER_EVENT.notificationShow, sinon.match.string);
expect(base.previewUI.notification.show).to.be.called;
expect(base.emit).to.be.calledWith(data.event, data.data);
expect(base.emit).to.be.calledWith('annotatorevent', data);
});
Expand All @@ -1271,7 +1272,7 @@ describe('lib/viewers/BaseViewer', () => {
};
base.handleAnnotatorEvents(data);
expect(base.disableViewerControls).to.be.called;
expect(base.emit).to.be.calledWith(VIEWER_EVENT.notificationShow, sinon.match.string);
expect(base.previewUI.notification.show).to.be.called;
expect(base.emit).to.be.calledWith(data.event, data.data);
expect(base.emit).to.be.calledWith('annotatorevent', data);
});
Expand All @@ -1285,7 +1286,7 @@ describe('lib/viewers/BaseViewer', () => {
};
base.handleAnnotatorEvents(data);
expect(base.enableViewerControls).to.be.called;
expect(base.emit).to.be.calledWith(VIEWER_EVENT.notificationHide);
expect(base.previewUI.notification.hide).to.be.called;
expect(base.emit).to.be.calledWith(data.event, data.data);
expect(base.emit).to.be.calledWith('annotatorevent', data);
});
Expand All @@ -1299,7 +1300,7 @@ describe('lib/viewers/BaseViewer', () => {
};
base.handleAnnotatorEvents(data);
expect(base.enableViewerControls).to.be.called;
expect(base.emit).to.be.calledWith(VIEWER_EVENT.notificationHide);
expect(base.previewUI.notification.hide).to.be.called;
expect(base.emit).to.be.calledWith(data.event, data.data);
expect(base.emit).to.be.calledWith('annotatorevent', data);
});
Expand All @@ -1310,7 +1311,7 @@ describe('lib/viewers/BaseViewer', () => {
data: 'message'
};
base.handleAnnotatorEvents(data);
expect(base.emit).to.be.calledWith(VIEWER_EVENT.notificationShow, data.data);
expect(base.previewUI.notification.show).to.be.called;
expect(base.emit).to.be.calledWith(data.event, data.data);
expect(base.emit).to.be.calledWith('annotatorevent', data);
});
Expand Down

0 comments on commit 0db8b4b

Please sign in to comment.