-
Notifications
You must be signed in to change notification settings - Fork 116
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
New: Log preview_error message from Preview #619
Changes from all commits
28c1cc2
4fa127c
f8d7960
5bffa55
d8e7a3f
7464a4a
f9463c5
5a299f1
73cb72a
6236dc5
0394df8
82c4e0a
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 |
---|---|---|
|
@@ -20,7 +20,8 @@ import { | |
getHeaders, | ||
findScriptLocation, | ||
appendQueryParams, | ||
replacePlaceholders | ||
replacePlaceholders, | ||
stripAuthFromString | ||
} from './util'; | ||
import { | ||
getURL, | ||
|
@@ -48,8 +49,9 @@ import { | |
X_REP_HINT_VIDEO_MP4, | ||
FILE_OPTION_FILE_VERSION_ID | ||
} from './constants'; | ||
import { VIEWER_EVENT } from './events'; | ||
import { VIEWER_EVENT, ERROR_CODE } from './events'; | ||
import './Preview.scss'; | ||
import { getClientLogDetails, createPreviewError, getISOTime } from './logUtils'; | ||
|
||
const DEFAULT_DISABLED_VIEWERS = ['Office']; // viewers disabled by default | ||
const PREFETCH_COUNT = 4; // number of files to prefetch | ||
|
@@ -330,9 +332,13 @@ class Preview extends EventEmitter { | |
if (checkFileValid(file)) { | ||
cacheFile(this.cache, file); | ||
} else { | ||
const message = '[Preview SDK] Tried to cache invalid file'; | ||
/* eslint-disable no-console */ | ||
console.error('[Preview SDK] Tried to cache invalid file: ', file); | ||
console.error(`${message}: `, file); | ||
/* eslint-enable no-console */ | ||
|
||
const err = createPreviewError(ERROR_CODE.invalidCacheAttempt, message, file); | ||
this.logPreviewError(err); | ||
} | ||
}); | ||
} | ||
|
@@ -536,6 +542,10 @@ class Preview extends EventEmitter { | |
/* eslint-disable no-console */ | ||
console.error(`Error prefetching file ID ${fileId} - ${err}`); | ||
/* eslint-enable no-console */ | ||
|
||
const error = createPreviewError(ERROR_CODE.prefetchFile, null, err); | ||
this.logPreviewError(error); | ||
|
||
return; | ||
} | ||
|
||
|
@@ -1051,6 +1061,10 @@ class Preview extends EventEmitter { | |
case VIEWER_EVENT.mediaEndAutoplay: | ||
this.navigateRight(); | ||
break; | ||
case VIEWER_EVENT.error: | ||
// Do nothing since 'error' event was already caught, and will be emitted | ||
// as a 'preview_error' event | ||
break; | ||
default: | ||
// This includes 'notification', 'preload' and others | ||
this.emit(data.event, data.data); | ||
|
@@ -1195,12 +1209,15 @@ class Preview extends EventEmitter { | |
|
||
// Check if hit the retry limit | ||
if (this.retryCount > RETRY_COUNT) { | ||
let errorCode = ERROR_CODE.retriesExceeded; | ||
let errorMessage = __('error_refresh'); | ||
if (err.response && err.response.status === 429) { | ||
errorCode = ERROR_CODE.rateLimit; | ||
errorMessage = __('error_rate_limit'); | ||
} | ||
|
||
this.triggerError(new Error(errorMessage)); | ||
const error = createPreviewError(errorCode, errorMessage, this.file.id); | ||
this.triggerError(error); | ||
return; | ||
} | ||
|
||
|
@@ -1245,6 +1262,9 @@ class Preview extends EventEmitter { | |
* @return {void} | ||
*/ | ||
triggerError(err) { | ||
// Always log preview errors | ||
this.logPreviewError(err); | ||
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 think there's a chance of double logging here. Above you bound to the 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. Aww hell. So, as it is, node requires that you bind to the 'error' event (listener #1), then we listen for it in Does it make sense that we begin listening for errors immediately on viewer creation, and not 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. We've confirmed that |
||
|
||
// If preview is closed don't do anything | ||
if (!this.open) { | ||
return; | ||
|
@@ -1269,6 +1289,50 @@ class Preview extends EventEmitter { | |
this.viewer.load(err); | ||
} | ||
|
||
/** | ||
* Create a generic log Object. | ||
* | ||
* @private | ||
* @return {Object} Log details for viewer session and current file. | ||
*/ | ||
createLog() { | ||
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. 👍 |
||
const file = this.file || {}; | ||
const log = { | ||
timestamp: getISOTime(), | ||
file_id: getProp(file, 'id', ''), | ||
file_version_id: getProp(file, 'file_version.id', ''), | ||
content_type: getProp(this.viewer, 'options.viewer.NAME', ''), | ||
extension: file.extension || '', | ||
locale: getProp(this.location, 'locale', ''), | ||
...getClientLogDetails() | ||
}; | ||
|
||
return log; | ||
} | ||
|
||
/** | ||
* Message, to any listeners of Preview, that an error has occurred. | ||
* | ||
* @private | ||
* @param {Error} error - The error that occurred. | ||
* @return {void} | ||
*/ | ||
logPreviewError(error) { | ||
const err = error; | ||
// If we haven't supplied a code, then it was thrown by the browser | ||
err.code = error.code || ERROR_CODE.browserError; | ||
// Make sure to strip auth, if it's a string. | ||
err.message = typeof error.message === 'string' ? stripAuthFromString(error.message) : error.message; | ||
err.displayMessage = typeof error.displayMessage === 'string' ? stripAuthFromString(error.displayMessage) : ''; | ||
|
||
const errorLog = { | ||
error: err, | ||
...this.createLog() | ||
}; | ||
|
||
this.emit('preview_error', errorLog); | ||
} | ||
|
||
/** | ||
* Builds a list of required XHR headers. | ||
* | ||
|
@@ -1344,16 +1408,27 @@ class Preview extends EventEmitter { | |
}); | ||
}) | ||
.catch((err) => { | ||
const message = `Error prefetching file ID ${fileId} - ${err}`; | ||
/* eslint-disable no-console */ | ||
console.error(`Error prefetching file ID ${fileId} - ${err}`); | ||
console.error(message); | ||
/* eslint-enable no-console */ | ||
|
||
const error = createPreviewError(ERROR_CODE.prefetchFile, message, { | ||
fileId, | ||
error: err | ||
}); | ||
this.logPreviewError(error); | ||
}); | ||
}); | ||
}) | ||
.catch(() => { | ||
const message = 'Error prefetching files'; | ||
/* eslint-disable no-console */ | ||
console.error('Error prefetching files'); | ||
console.error(message); | ||
/* eslint-enable no-console */ | ||
|
||
const error = createPreviewError(ERROR_CODE, message, filesToPrefetch); | ||
this.logPreviewError(error); | ||
}); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import EventEmitter from 'events'; | ||
import { get, appendAuthParams } from './util'; | ||
import { STATUS_SUCCESS, STATUS_VIEWABLE } from './constants'; | ||
import { createPreviewError } from './logUtils'; | ||
|
||
const STATUS_UPDATE_INTERVAL_MS = 2000; | ||
|
||
|
@@ -106,26 +107,29 @@ class RepStatus extends EventEmitter { | |
*/ | ||
handleResponse() { | ||
const status = RepStatus.getStatus(this.representation); | ||
let errorCode; | ||
const errCode = RepStatus.getErrorCode(this.representation); | ||
let errorMessage; | ||
let error; | ||
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. This section changed to allow readable error codes with translated display messages |
||
|
||
switch (status) { | ||
case 'error': | ||
switch (RepStatus.getErrorCode(this.representation)) { | ||
switch (errCode) { | ||
case ERROR_PASSWORD_PROTECTED: | ||
errorCode = __('error_password_protected'); | ||
errorMessage = __('error_password_protected'); | ||
break; | ||
case ERROR_TRY_AGAIN_LATER: | ||
errorCode = __('error_try_again_later'); | ||
errorMessage = __('error_try_again_later'); | ||
break; | ||
case ERROR_UNSUPPORTED_FORMAT: | ||
errorCode = __('error_bad_file'); | ||
errorMessage = __('error_bad_file'); | ||
break; | ||
default: | ||
errorCode = __('error_refresh'); | ||
errorMessage = __('error_refresh'); | ||
break; | ||
} | ||
|
||
this.reject(errorCode); | ||
error = createPreviewError(errCode, errorMessage, this.representation); | ||
this.reject(error); | ||
break; | ||
|
||
case STATUS_SUCCESS: | ||
|
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.
Can we remove this case entirely?
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.
We cannot. This prevents a double error message from being emitted, which would double up on firing off preview_error events.