diff --git a/src/lib/Preview.js b/src/lib/Preview.js index b5832b5eb..08b5ffae2 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -1056,10 +1056,6 @@ class Preview extends EventEmitter { // Destroy anything still showing this.destroy(); - // Figure out what error message to log and what error message to display - const logMessage = err instanceof Error ? err.message : __('error_default'); - const displayMessage = err && err.displayMessage ? err.displayMessage : logMessage; - // Instantiate the error viewer this.viewer = this.getErrorViewer(); @@ -1067,7 +1063,7 @@ class Preview extends EventEmitter { this.attachViewerListeners(); // Load the error viewer - this.viewer.load(displayMessage); + this.viewer.load(err); } /** diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index c9ed6fbbe..abb6d58c4 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -1696,11 +1696,12 @@ describe('lib/Preview', () => { }); it('should get the error viewer, attach viewer listeners, and load the error viewer', () => { - preview.triggerError(); + const err = new Error(); + preview.triggerError(err); expect(stubs.getErrorViewer).to.be.called; expect(stubs.attachViewerListeners).to.be.called; - expect(ErrorViewer.load).to.be.called; + expect(ErrorViewer.load).to.be.calledWith(err); }); }); diff --git a/src/lib/viewers/doc/DocBaseViewer.js b/src/lib/viewers/doc/DocBaseViewer.js index 03109df44..8c43f91cc 100644 --- a/src/lib/viewers/doc/DocBaseViewer.js +++ b/src/lib/viewers/doc/DocBaseViewer.js @@ -501,6 +501,7 @@ class DocBaseViewer extends BaseViewer { if (err instanceof Error) { error.displayMessage = __('error_document'); } + this.triggerError(err); }); } diff --git a/src/lib/viewers/error/PreviewErrorViewer.js b/src/lib/viewers/error/PreviewErrorViewer.js index 8c344fc44..27fe139f7 100644 --- a/src/lib/viewers/error/PreviewErrorViewer.js +++ b/src/lib/viewers/error/PreviewErrorViewer.js @@ -52,15 +52,14 @@ class PreviewErrorViewer extends BaseViewer { /** * Shows an error message to the user. * - * @param {string} reason - Error reason + * @param {Error} err - Error * @return {void} */ - load(reason) { + load(err) { this.setup(); const { file, showDownload } = this.options; let icon = ICON_FILE_DEFAULT; - const message = reason || __('error_default'); // Generic errors will not have the file object if (file) { @@ -77,8 +76,15 @@ class PreviewErrorViewer extends BaseViewer { } } + /* eslint-disable no-param-reassign */ + err = err instanceof Error ? err : new Error(__('error_default')); + /* eslint-enable no-param-reassign */ + + // If there is no display message fallback to the message from above + const displayMessage = err.displayMessage || err.message; + this.iconEl.innerHTML = icon; - this.messageEl.textContent = message; + this.messageEl.textContent = displayMessage; // Add optional download button if (checkPermission(file, PERMISSION_DOWNLOAD) && showDownload && Browser.canDownload()) { @@ -86,8 +92,11 @@ class PreviewErrorViewer extends BaseViewer { } this.loaded = true; + + // The error will either be the message from the original error, the displayMessage from the orignal error, + // or the default message from the locally created error this.emit('load', { - error: message + error: err.message || displayMessage }); } diff --git a/src/lib/viewers/error/__tests__/PreviewErrorViewer-test.js b/src/lib/viewers/error/__tests__/PreviewErrorViewer-test.js index be26d96fc..1c4b44d7f 100644 --- a/src/lib/viewers/error/__tests__/PreviewErrorViewer-test.js +++ b/src/lib/viewers/error/__tests__/PreviewErrorViewer-test.js @@ -64,12 +64,13 @@ describe('lib/viewers/error/PreviewErrorViewer', () => { const extension = testCase[0]; const expectedIcon = testCase[1]; - const message = 'reason'; + const err = new Error(); + err.displayMessage = 'reason'; error.options.file.extension = extension; - error.load(message); + error.load(err); expect(error.iconEl).to.contain.html(expectedIcon); - expect(error.messageEl.textContent).to.equal(message); + expect(error.messageEl.textContent).to.equal(err.displayMessage); }); }); @@ -112,17 +113,36 @@ describe('lib/viewers/error/PreviewErrorViewer', () => { expect(error.addDownloadButton).to.not.be.called; }); - it('should broadcast load', () => { + it('should broadcast the log message', () => { sandbox.stub(error, 'emit'); - const message = 'reason'; - error.load(message); + const err = new Error(); + err.displayMessage = 'reason'; + err.message = 'this is bad'; + + error.load(err); + + expect(error.emit).to.be.calledWith( + 'load', { + error: 'this is bad' + } + ); + }); + + it('should broadcast the display message if there is no log message', () => { + sandbox.stub(error, 'emit'); + + const err = new Error(); + err.displayMessage = 'reason'; + err.message = undefined; + + error.load(err); + expect(error.emit).to.be.calledWith( - 'load', - sinon.match({ - error: message - }) + 'load', { + error: 'reason' + } ); }); });