javascript: make Uploader always throw the same kind of errors

A DirectUpload may fail for several reasons, and return many types of
errors (string, xhr response, Error objects, etc).

For convenience, wrap all these errors in a FileUploadError object.

- It makes easier for clients of the Uploader class to handle errors;
- It allows to propagate the error code and failure responsability.
This commit is contained in:
Pierre de La Morinerie 2020-04-15 16:29:15 +02:00
parent d8f3b86b0e
commit 432967bd76
5 changed files with 92 additions and 53 deletions

View file

@ -18,6 +18,8 @@ export default class AutoUploadController {
);
}
// Create, upload and attach the file.
// On failure, display an error message and throw a FileUploadError.
async start() {
try {
this._begin();
@ -55,36 +57,15 @@ export default class AutoUploadController {
this.input.disabled = false;
}
_isError422(error) {
// Ajax errors have an xhr attribute
if (error && error.xhr && error.xhr.status == 422) return true;
// Rails DirectUpload errors are returned as a String, e.g. 'Error creating Blob for "Demain.txt". Status: 422'
if (error && error.toString().includes('422')) return true;
return false;
}
_messageFromError(error) {
let allowRetry = !this._isError422(error);
let message = error.message || error.toString();
let canRetry = error.status && error.status != 422;
if (
error.xhr &&
error.xhr.status == 422 &&
error.response &&
error.response.errors &&
error.response.errors[0]
) {
return {
title: error.response.errors[0],
description: '',
retry: allowRetry
};
} else {
return {
title: 'Une erreur sest produite pendant lenvoi du fichier.',
description: error.message || error.toString(),
retry: allowRetry
};
return {
title: 'Le fichier na pas pu être envoyé.',
description: message,
retry: canRetry
}
}