Skip to content

Commit f516986

Browse files
author
Vincent Petry
authored
Merge pull request #28635 from owncloud/stable10-parse-sabre-exception-upload
[stable10] Parse Sabre Exception in OC.Files.Client and file-upload
2 parents 72c43e6 + 2e1cf51 commit f516986

File tree

3 files changed

+82
-18
lines changed

3 files changed

+82
-18
lines changed

apps/files/js/file-upload.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,23 @@ OC.FileUpload.prototype = {
327327
*/
328328
getResponse: function() {
329329
var response = this.data.response();
330-
if (typeof response.result !== 'string') {
330+
if (response.errorThrown) {
331+
// attempt parsing Sabre exception is available
332+
var xml = response.jqXHR.responseXML;
333+
if (xml.documentElement.localName === 'error' && xml.documentElement.namespaceURI === 'DAV:') {
334+
var messages = xml.getElementsByTagNameNS('http://sabredav.org/ns', 'message');
335+
var exceptions = xml.getElementsByTagNameNS('http://sabredav.org/ns', 'exception');
336+
if (messages.length) {
337+
response.message = messages[0].textContent;
338+
}
339+
if (exceptions.length) {
340+
response.exception = exceptions[0].textContent;
341+
}
342+
return response;
343+
}
344+
}
345+
346+
if (typeof response.result !== 'string' && response.result) {
331347
//fetch response from iframe
332348
response = $.parseJSON(response.result[0].body.innerText);
333349
if (!response) {
@@ -976,6 +992,7 @@ OC.Uploader.prototype = _.extend({
976992
status = upload.getResponseStatus();
977993
}
978994
self.log('fail', e, upload);
995+
self._hideProgressBar();
979996

980997
if (data.textStatus === 'abort') {
981998
self.showUploadCancelMessage();
@@ -997,7 +1014,12 @@ OC.Uploader.prototype = _.extend({
9971014
self.cancelUploads();
9981015
} else {
9991016
// HTTP connection problem or other error
1000-
OC.Notification.show(data.errorThrown, {type: 'error'});
1017+
var message = '';
1018+
if (upload) {
1019+
var response = upload.getResponse();
1020+
message = response.message;
1021+
}
1022+
OC.Notification.show(message || data.errorThrown, {type: 'error'});
10011023
}
10021024

10031025
if (upload) {
@@ -1139,16 +1161,17 @@ OC.Uploader.prototype = _.extend({
11391161
upload.done().then(function() {
11401162
self._hideProgressBar();
11411163
self.trigger('done', e, upload);
1142-
}).fail(function(status) {
1164+
}).fail(function(status, response) {
1165+
var message = response.message;
11431166
self._hideProgressBar();
11441167
if (status === 507) {
11451168
// not enough space
1146-
OC.Notification.show(t('files', 'Not enough free space'), {type: 'error'});
1169+
OC.Notification.show(message || t('files', 'Not enough free space'), {type: 'error'});
11471170
self.cancelUploads();
11481171
} else if (status === 409) {
1149-
OC.Notification.show(t('files', 'Target folder does not exist any more'), {type: 'error'});
1172+
OC.Notification.show(message || t('files', 'Target folder does not exist any more'), {type: 'error'});
11501173
} else {
1151-
OC.Notification.show(t('files', 'Error when assembling chunks, status code {status}', {status: status}), {type: 'error'});
1174+
OC.Notification.show(message || t('files', 'Error when assembling chunks, status code {status}', {status: status}), {type: 'error'});
11521175
}
11531176
self.trigger('fail', e, data);
11541177
});

core/js/files/client.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,26 @@
372372
return status >= 200 && status <= 299;
373373
},
374374

375+
/**
376+
* Parse the Sabre exception out of the given response, if any
377+
*
378+
* @param {Object} response object
379+
* @return {Object} array of parsed message and exception (only the first one)
380+
*/
381+
_getSabreException: function(response) {
382+
var result = {};
383+
var xml = response.xhr.responseXML;
384+
var messages = xml.getElementsByTagNameNS('http://sabredav.org/ns', 'message');
385+
var exceptions = xml.getElementsByTagNameNS('http://sabredav.org/ns', 'exception');
386+
if (messages.length) {
387+
result.message = messages[0].textContent;
388+
}
389+
if (exceptions.length) {
390+
result.exception = exceptions[0].textContent;
391+
}
392+
return result;
393+
},
394+
375395
/**
376396
* Returns the default PROPFIND properties to use during a call.
377397
*
@@ -425,7 +445,8 @@
425445
}
426446
deferred.resolve(result.status, results);
427447
} else {
428-
deferred.reject(result.status);
448+
result = _.extend(result, self._getSabreException(result));
449+
deferred.reject(result.status, result);
429450
}
430451
});
431452
return promise;
@@ -499,7 +520,8 @@
499520
var results = self._parseResult(result.body);
500521
deferred.resolve(result.status, results);
501522
} else {
502-
deferred.reject(result.status);
523+
result = _.extend(result, self._getSabreException(result));
524+
deferred.reject(result.status, result);
503525
}
504526
});
505527
return promise;
@@ -538,7 +560,8 @@
538560
if (self._isSuccessStatus(result.status)) {
539561
deferred.resolve(result.status, self._parseResult([result.body])[0]);
540562
} else {
541-
deferred.reject(result.status);
563+
result = _.extend(result, self._getSabreException(result));
564+
deferred.reject(result.status, result);
542565
}
543566
}
544567
);
@@ -568,7 +591,8 @@
568591
if (self._isSuccessStatus(result.status)) {
569592
deferred.resolve(result.status, result.body);
570593
} else {
571-
deferred.reject(result.status);
594+
result = _.extend(result, self._getSabreException(result));
595+
deferred.reject(result.status, result);
572596
}
573597
}
574598
);
@@ -617,7 +641,8 @@
617641
if (self._isSuccessStatus(result.status)) {
618642
deferred.resolve(result.status);
619643
} else {
620-
deferred.reject(result.status);
644+
result = _.extend(result, self._getSabreException(result));
645+
deferred.reject(result.status, result);
621646
}
622647
}
623648
);
@@ -641,7 +666,8 @@
641666
if (self._isSuccessStatus(result.status)) {
642667
deferred.resolve(result.status);
643668
} else {
644-
deferred.reject(result.status);
669+
result = _.extend(result, self._getSabreException(result));
670+
deferred.reject(result.status, result);
645671
}
646672
}
647673
);
@@ -705,11 +731,12 @@
705731
this._buildUrl(path),
706732
headers
707733
).then(
708-
function(response) {
709-
if (self._isSuccessStatus(response.status)) {
710-
deferred.resolve(response.status);
734+
function(result) {
735+
if (self._isSuccessStatus(result.status)) {
736+
deferred.resolve(result.status);
711737
} else {
712-
deferred.reject(response.status);
738+
result = _.extend(result, self._getSabreException(result));
739+
deferred.reject(result.status, result);
713740
}
714741
}
715742
);

core/js/tests/specs/files/clientSpec.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,28 @@ describe('OC.Files.Client tests', function() {
8787
promise.done(successHandler);
8888
promise.fail(failHandler);
8989

90+
var errorXml =
91+
'<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">' +
92+
' <s:exception>Sabre\\DAV\\Exception\\SomeException</s:exception>' +
93+
' <s:message>Some error message</s:message>' +
94+
'</d:error>';
95+
96+
var parser = new DOMParser();
97+
9098
requestDeferred.resolve({
9199
status: status,
92-
body: ''
100+
body: errorXml,
101+
xhr: {
102+
responseXML: parser.parseFromString(errorXml, 'application/xml')
103+
}
93104
});
94105

95106
promise.then(function() {
96107
expect(failHandler.calledOnce).toEqual(true);
97-
expect(failHandler.calledWith(status)).toEqual(true);
108+
expect(failHandler.getCall(0).args[0]).toEqual(status);
109+
expect(failHandler.getCall(0).args[1].status).toEqual(status);
110+
expect(failHandler.getCall(0).args[1].message).toEqual('Some error message');
111+
expect(failHandler.getCall(0).args[1].exception).toEqual('Sabre\\DAV\\Exception\\SomeException');
98112

99113
expect(successHandler.notCalled).toEqual(true);
100114
});

0 commit comments

Comments
 (0)