-
Notifications
You must be signed in to change notification settings - Fork 91
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
More error handling in the polling client #43
Changes from all commits
0ca5e10
530e992
1163e25
ab1d3ec
b019752
cf44f9e
1b141a1
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 |
---|---|---|
|
@@ -10,7 +10,11 @@ var CeleryProgressBar = (function () { | |
} | ||
} | ||
|
||
function onErrorDefault(progressBarElement, progressBarMessageElement, excMessage) { | ||
/** | ||
* Default handler for all errors. | ||
* @param data - A Response object for HTTP errors, undefined for other errors | ||
*/ | ||
function onErrorDefault(progressBarElement, progressBarMessageElement, excMessage, data) { | ||
progressBarElement.style.backgroundColor = '#dc4f63'; | ||
progressBarMessageElement.innerHTML = "Uh-Oh, something went wrong! " + excMessage; | ||
} | ||
|
@@ -22,7 +26,7 @@ var CeleryProgressBar = (function () { | |
progressBarMessageElement.innerHTML = progress.current + ' of ' + progress.total + ' processed. ' + description; | ||
} | ||
|
||
function updateProgress (progressUrl, options) { | ||
async function updateProgress (progressUrl, options) { | ||
options = options || {}; | ||
var progressBarId = options.progressBarId || 'progress-bar'; | ||
var progressBarMessage = options.progressBarMessageId || 'progress-bar-message'; | ||
|
@@ -31,31 +35,53 @@ var CeleryProgressBar = (function () { | |
var onProgress = options.onProgress || onProgressDefault; | ||
var onSuccess = options.onSuccess || onSuccessDefault; | ||
var onError = options.onError || onErrorDefault; | ||
var onTaskError = options.onTaskError || onError; | ||
var onNetworkError = options.onNetworkError || onError; | ||
var onHttpError = options.onHttpError || onError; | ||
var onDataError = options.onDataError || onError; | ||
var pollInterval = options.pollInterval || 500; | ||
var resultElementId = options.resultElementId || 'celery-result'; | ||
var resultElement = options.resultElement || document.getElementById(resultElementId); | ||
var onResult = options.onResult || onResultDefault; | ||
|
||
|
||
fetch(progressUrl).then(function(response) { | ||
response.json().then(function(data) { | ||
if (data.progress) { | ||
onProgress(progressBarElement, progressBarMessageElement, data.progress); | ||
} | ||
if (!data.complete) { | ||
setTimeout(updateProgress, pollInterval, progressUrl, options); | ||
let response; | ||
try { | ||
response = await fetch(progressUrl); | ||
} catch (networkError) { | ||
onNetworkError(progressBarElement, progressBarMessageElement, "Network Error"); | ||
throw networkError; | ||
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. what's the value in re-throwing this one (and 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.
This is the console if I throw networkError: If I don't throw networkError: If I don't throw parsingError: If we don't want to throw, we could either:
|
||
} | ||
|
||
if (response.status === 200) { | ||
let data; | ||
try { | ||
data = await response.json(); | ||
} catch (parsingError) { | ||
onDataError(progressBarElement, progressBarMessageElement, "Parsing Error") | ||
throw parsingError; | ||
} | ||
|
||
if (data.progress) { | ||
onProgress(progressBarElement, progressBarMessageElement, data.progress); | ||
} | ||
if (data.complete === false) { | ||
setTimeout(updateProgress, pollInterval, progressUrl, options); | ||
} else { | ||
if (data.success === true) { | ||
onSuccess(progressBarElement, progressBarMessageElement, data.result); | ||
} else if (data.success === false) { | ||
onTaskError(progressBarElement, progressBarMessageElement, data.result); | ||
} else { | ||
if (data.success) { | ||
onSuccess(progressBarElement, progressBarMessageElement, data.result); | ||
} else { | ||
onError(progressBarElement, progressBarMessageElement, data.result); | ||
} | ||
if (data.result) { | ||
onResult(resultElement, data.result); | ||
} | ||
onDataError(progressBarElement, progressBarMessageElement, "Data Error"); | ||
} | ||
if (data.hasOwnProperty('result')) { | ||
onResult(resultElement, data.result); | ||
} | ||
}); | ||
}); | ||
} | ||
} else { | ||
onHttpError(progressBarElement, progressBarMessageElement, "HTTP Code " + response.status, response); | ||
} | ||
} | ||
return { | ||
onSuccessDefault: onSuccessDefault, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,8 @@ var CeleryWebSocketProgressBar = (function () { | |
CeleryProgressBar.onResultDefault(resultElement, result); | ||
} | ||
|
||
function onErrorDefault(progressBarElement, progressBarMessageElement, excMessage) { | ||
CeleryProgressBar.onErrorDefault(progressBarElement, progressBarMessageElement, excMessage); | ||
function onErrorDefault(progressBarElement, progressBarMessageElement, excMessage, data) { | ||
CeleryProgressBar.onErrorDefault(progressBarElement, progressBarMessageElement, excMessage, data); | ||
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. @EJH2 do you have any thoughts on this part? I also have never used the websockets client. |
||
} | ||
|
||
function onProgressDefault(progressBarElement, progressBarMessageElement, progress) { | ||
|
@@ -24,6 +24,7 @@ var CeleryWebSocketProgressBar = (function () { | |
var onProgress = options.onProgress || onProgressDefault; | ||
var onSuccess = options.onSuccess || onSuccessDefault; | ||
var onError = options.onError || onErrorDefault; | ||
var onTaskError = options.onTaskError || onError; | ||
var resultElementId = options.resultElementId || 'celery-result'; | ||
var resultElement = options.resultElement || document.getElementById(resultElementId); | ||
var onResult = options.onResult || onResultDefault; | ||
|
@@ -45,9 +46,9 @@ var CeleryWebSocketProgressBar = (function () { | |
if (data.success) { | ||
onSuccess(progressBarElement, progressBarMessageElement, data.result); | ||
} else { | ||
onError(progressBarElement, progressBarMessageElement, data.result); | ||
onTaskError(progressBarElement, progressBarMessageElement, data.result); | ||
} | ||
if (data.result) { | ||
if (data.hasOwnProperty('result')) { | ||
onResult(resultElement, data.result); | ||
} | ||
ProgressSocket.close(); | ||
|
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.
👍 thanks for updating this