Skip to content

Commit eed0149

Browse files
authored
ENGCOM-8302: Adding failure callback to ui file uploader #27092
2 parents c0088d3 + a38f936 commit eed0149

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

app/code/Magento/Ui/view/base/web/js/form/element/file-uploader.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ define([
5454
this.$fileInput = fileInput;
5555

5656
_.extend(this.uploaderConfig, {
57-
dropZone: $(fileInput).closest(this.dropZone),
58-
change: this.onFilesChoosed.bind(this),
59-
drop: this.onFilesChoosed.bind(this),
60-
add: this.onBeforeFileUpload.bind(this),
61-
done: this.onFileUploaded.bind(this),
62-
start: this.onLoadingStart.bind(this),
63-
stop: this.onLoadingStop.bind(this)
57+
dropZone: $(fileInput).closest(this.dropZone),
58+
change: this.onFilesChoosed.bind(this),
59+
drop: this.onFilesChoosed.bind(this),
60+
add: this.onBeforeFileUpload.bind(this),
61+
fail: this.onFail.bind(this),
62+
done: this.onFileUploaded.bind(this),
63+
start: this.onLoadingStart.bind(this),
64+
stop: this.onLoadingStop.bind(this)
6465
});
6566

6667
$(fileInput).fileupload(this.uploaderConfig);
@@ -328,11 +329,12 @@ define([
328329
* May be used for implementation of additional validation rules,
329330
* e.g. total files and a total size rules.
330331
*
331-
* @param {Event} e - Event object.
332+
* @param {Event} event - Event object.
332333
* @param {Object} data - File data that will be uploaded.
333334
*/
334-
onFilesChoosed: function (e, data) {
335-
// no option exists in fileuploader for restricting upload chains to single files; this enforces that policy
335+
onFilesChoosed: function (event, data) {
336+
// no option exists in file uploader for restricting upload chains to single files
337+
// this enforces that policy
336338
if (!this.isMultipleFiles) {
337339
data.files.splice(1);
338340
}
@@ -341,13 +343,13 @@ define([
341343
/**
342344
* Handler which is invoked prior to the start of a file upload.
343345
*
344-
* @param {Event} e - Event object.
346+
* @param {Event} event - Event object.
345347
* @param {Object} data - File data that will be uploaded.
346348
*/
347-
onBeforeFileUpload: function (e, data) {
348-
var file = data.files[0],
349-
allowed = this.isFileAllowed(file),
350-
target = $(e.target);
349+
onBeforeFileUpload: function (event, data) {
350+
var file = data.files[0],
351+
allowed = this.isFileAllowed(file),
352+
target = $(event.target);
351353

352354
if (this.disabled()) {
353355
this.notifyError($t('The file upload field is disabled.'));
@@ -356,7 +358,7 @@ define([
356358
}
357359

358360
if (allowed.passed) {
359-
target.on('fileuploadsend', function (event, postData) {
361+
target.on('fileuploadsend', function (eventBound, postData) {
360362
postData.data.append('param_name', this.paramName);
361363
}.bind(data));
362364

@@ -386,16 +388,25 @@ define([
386388
});
387389
},
388390

391+
/**
392+
* @param {Event} event
393+
* @param {Object} data
394+
*/
395+
onFail: function (event, data) {
396+
console.error(data.jqXHR.responseText);
397+
console.error(data.jqXHR.status);
398+
},
399+
389400
/**
390401
* Handler of the file upload complete event.
391402
*
392-
* @param {Event} e
403+
* @param {Event} event
393404
* @param {Object} data
394405
*/
395-
onFileUploaded: function (e, data) {
406+
onFileUploaded: function (event, data) {
396407
var uploadedFilename = data.files[0].name,
397-
file = data.result,
398-
error = file.error;
408+
file = data.result,
409+
error = file.error;
399410

400411
error ?
401412
this.aggregateError(uploadedFilename, error) :
@@ -469,10 +480,10 @@ define([
469480
* Handler of the preview image load event.
470481
*
471482
* @param {Object} file - File associated with an image.
472-
* @param {Event} e
483+
* @param {Event} event
473484
*/
474-
onPreviewLoad: function (file, e) {
475-
var img = e.currentTarget;
485+
onPreviewLoad: function (file, event) {
486+
var img = event.currentTarget;
476487

477488
file.previewWidth = img.naturalWidth;
478489
file.previewHeight = img.naturalHeight;

dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/element/file-uploader.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,27 @@ define([
358358
});
359359
});
360360

361+
describe('onFail handler', function () {
362+
it('it logs responseText and status', function () {
363+
var fakeEvent = {
364+
target: document.createElement('input')
365+
},
366+
data = {
367+
jqXHR: {
368+
responseText: 'Failed',
369+
status: '500'
370+
}
371+
};
372+
373+
spyOn(console, 'error');
374+
375+
component.onFail(fakeEvent, data);
376+
expect(console.error).toHaveBeenCalledWith(data.jqXHR.responseText);
377+
expect(console.error).toHaveBeenCalledWith(data.jqXHR.status);
378+
expect(console.error).toHaveBeenCalledTimes(2);
379+
});
380+
});
381+
361382
describe('aggregateError method', function () {
362383
it('should append onto aggregatedErrors array when called', function () {
363384
spyOn(component.aggregatedErrors, 'push');

0 commit comments

Comments
 (0)