-
Notifications
You must be signed in to change notification settings - Fork 113
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
Update: Add un-downloadable error case #667
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ const KEYDOWN_EXCEPTIONS = ['INPUT', 'SELECT', 'TEXTAREA']; // Ignore keydown ev | |
const LOG_RETRY_TIMEOUT_MS = 500; // retry interval for logging preview event | ||
const LOG_RETRY_COUNT = 3; // number of times to retry logging preview event | ||
const MS_IN_S = 1000; // ms in a sec | ||
const SUPPORT_URL = 'https://support.box.com'; | ||
|
||
// All preview assets are relative to preview.js. Here we create a location | ||
// object that mimics the window location object and points to where | ||
|
@@ -923,6 +924,16 @@ class Preview extends EventEmitter { | |
this.file = file; | ||
this.logger.setFile(file); | ||
|
||
// If file is not downloadable, trigger an error | ||
if (file.is_download_available === false) { | ||
const error = createPreviewError(ERROR_CODE.notDownloadable, __('error_not_downloadable'), null, { | ||
linkText: __('link_contact_us'), | ||
linkUrl: SUPPORT_URL | ||
}); | ||
this.triggerError(error); | ||
return; | ||
} | ||
|
||
// Keep reference to previously cached file version | ||
const cachedFile = getCachedFile(this.cache, { fileVersionId: responseFileVersionId }); | ||
|
||
|
@@ -1497,7 +1508,7 @@ class Preview extends EventEmitter { | |
console.error(message); | ||
/* eslint-enable no-console */ | ||
|
||
const error = createPreviewError(ERROR_CODE, message, filesToPrefetch); | ||
const error = createPreviewError(ERROR_CODE.prefetchFile, message, filesToPrefetch); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JustinHoldstock we also want a specific code here right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I apologize for missing that one. Thanks for catching it |
||
this.emitPreviewError(error); | ||
}); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,13 +50,20 @@ export function getClientLogDetails() { | |
* @param {string} code - Code associated with the error that occurred. | ||
* @param {string} [displayMessage] - Optional string to display, if applicable. | ||
* @param {*} [details] - Additional details about the error. IE) File id, reason, etc. | ||
* @param {*} [properties] - Additional optional properties on the error | ||
* @return {Error} An error object with an associated error code. | ||
*/ | ||
export function createPreviewError(code, displayMessage = __('error_refresh'), details = null) { | ||
export function createPreviewError(code, displayMessage = __('error_refresh'), details = null, properties) { | ||
const error = new Error(code); | ||
error.code = code; | ||
error.message = details || displayMessage; | ||
error.displayMessage = displayMessage; | ||
|
||
if (properties) { | ||
Object.keys(properties).forEach((key) => { | ||
error[key] = properties[key]; | ||
}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JustinHoldstock I wonder if there's an opportunity to combine Open to other thoughts here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say anything you want logged to the backend needs to be stored in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One other note, I'm quite opposed to us dynamically creating properties on an already defined object type ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JustinHoldstock are you okay with my merging this as-is and then following up with a refactor? There are a lot of places I'd have to touch for a Previewer for refactor and 1) I'd like to get this change in before the release cut tomorrow and 2) I probably want that larger change in a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely! :D |
||
|
||
return error; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,10 +26,13 @@ class PreviewErrorViewer extends BaseViewer { | |
super.setup(); | ||
|
||
this.infoEl = this.containerEl.appendChild(document.createElement('div')); | ||
this.infoEl.className = 'bp-error'; | ||
|
||
this.iconEl = this.infoEl.appendChild(document.createElement('div')); | ||
this.iconEl.className = 'bp-icon bp-icon-file'; | ||
|
||
this.messageEl = this.infoEl.appendChild(document.createElement('div')); | ||
this.infoEl.className = 'bp-error'; | ||
this.messageEl.className = 'bp-error-text'; | ||
} | ||
|
||
/** | ||
|
@@ -91,8 +94,11 @@ class PreviewErrorViewer extends BaseViewer { | |
this.iconEl.innerHTML = this.icon; | ||
this.messageEl.textContent = displayMessage; | ||
|
||
// Add optional download button | ||
if (checkPermission(file, PERMISSION_DOWNLOAD) && showDownload && Browser.canDownload()) { | ||
// Add optional link or download button | ||
const { linkText, linkUrl } = err; | ||
if (linkText && linkUrl) { | ||
this.addLinkButton(linkText, linkUrl); | ||
} else if (checkPermission(file, PERMISSION_DOWNLOAD) && showDownload && Browser.canDownload()) { | ||
this.addDownloadButton(); | ||
} | ||
|
||
|
@@ -111,17 +117,29 @@ class PreviewErrorViewer extends BaseViewer { | |
} | ||
|
||
/** | ||
* Adds optional download button | ||
* Adds a link button underneath error message. | ||
* | ||
* @param {string} linkText - Translated button message | ||
* @param {string} linkUrl - URL for link | ||
* @return {void} | ||
*/ | ||
addLinkButton(linkText, linkUrl) { | ||
const linkBtnEl = this.infoEl.appendChild(document.createElement('a')); | ||
linkBtnEl.className = 'bp-btn bp-btn-primary'; | ||
linkBtnEl.target = '_blank'; | ||
linkBtnEl.textContent = linkText; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be localized too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add documentation to the JSDoc that this should be localized before passed in. |
||
linkBtnEl.href = linkUrl; | ||
} | ||
|
||
/** | ||
* Adds a download file button underneath error message. | ||
* | ||
* @private | ||
* @return {void} | ||
*/ | ||
addDownloadButton() { | ||
this.downloadEl = this.infoEl.appendChild(document.createElement('div')); | ||
this.downloadEl.classList.add('bp-error-download'); | ||
this.downloadBtnEl = this.downloadEl.appendChild(document.createElement('button')); | ||
this.downloadBtnEl.classList.add('bp-btn'); | ||
this.downloadBtnEl.classList.add('bp-btn-primary'); | ||
this.downloadBtnEl = this.infoEl.appendChild(document.createElement('button')); | ||
this.downloadBtnEl.className = 'bp-btn bp-btn-primary'; | ||
this.downloadBtnEl.textContent = __('download'); | ||
this.downloadBtnEl.addEventListener('click', this.download); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JustinHoldstock am I using
createPreviewError()
right here? I'm not clear on what the third param will be used for - I see it can be any type, string, obj, Error. Are we parsing that at the WebApp level?Also
const error = createPreviewError(ERROR_CODE.prefetchFile, null, err);
on https://github.com/box/box-content-preview/blob/master/src/lib/Preview.js#L550 might not be doing what you expect sincenull
will be explicitly assigned todisplayMessage
. Default function parameters are only used when you pass no value orundefined
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parametersThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The third param should be the details of the error. You can remove the third param,
null
.The second param of the function is set to the
displayMessage
property of the error created. The third is set to themessage
property of the error created.Aww hell, I forgot that
null
wouldn't work with default params. I will make a ticket to update anywhere usingnull
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just saw what you did there, with the fourth param. See comment below