]> source.dussan.org Git - nextcloud-server.git/commitdiff
Properly forward error messages in share dialog
authorVincent Petry <pvince81@owncloud.com>
Thu, 28 Jan 2016 16:07:51 +0000 (17:07 +0100)
committerVincent Petry <pvince81@owncloud.com>
Thu, 28 Jan 2016 16:18:33 +0000 (17:18 +0100)
core/js/shareitemmodel.js
core/js/tests/specs/shareitemmodelSpec.js

index c9fc85c91b2832d839db4db07b64e2a0c40957d6..a7764dd6266f67723a19b60de607202cde4084dc 100644 (file)
                                shareId = this.get('linkShare').id;
 
                                // note: update can only update a single value at a time
-                               call = this.updateShare(shareId, attributes);
+                               call = this.updateShare(shareId, attributes, options);
                        } else {
                                attributes = _.defaults(attributes, {
                                        password: '',
                                        shareType: OC.Share.SHARE_TYPE_LINK
                                });
 
-                               call = this.addShare(attributes);
+                               call = this.addShare(attributes, options);
                        }
 
                        return call;
                                                }
                                        }
                                });
-                       }).fail(function(result) {
+                       }).fail(function(xhr) {
                                var msg = t('core', 'Error');
+                               var result = xhr.responseJSON;
                                if (result.ocs && result.ocs.meta) {
                                        msg = result.ocs.meta.message;
                                }
                        });
                },
 
-               updateShare: function(shareId, attrs) {
+               updateShare: function(shareId, attrs, options) {
                        var self = this;
+                       options = options || {};
                        return $.ajax({
                                type: 'PUT',
                                url: this._getUrl('shares/' + encodeURIComponent(shareId)),
                                data: attrs,
                                dataType: 'json'
                        }).done(function() {
-                               self.fetch();
+                               self.fetch({
+                                       success: function() {
+                                               if (_.isFunction(options.success)) {
+                                                       options.success(self);
+                                               }
+                                       }
+                               });
+                       }).fail(function(xhr) {
+                               var msg = t('core', 'Error');
+                               var result = xhr.responseJSON;
+                               if (result.ocs && result.ocs.meta) {
+                                       msg = result.ocs.meta.message;
+                               }
+
+                               if (_.isFunction(options.error)) {
+                                       options.error(self, msg);
+                               } else {
+                                       OC.dialogs.alert(msg, t('core', 'Error while sharing'));
+                               }
                        });
                },
 
                 * @param {int} shareId share id
                 * @return {jQuery}
                 */
-               removeShare: function(shareId) {
+               removeShare: function(shareId, options) {
                        var self = this;
+                       options = options || {};
                        return $.ajax({
                                type: 'DELETE',
                                url: this._getUrl('shares/' + encodeURIComponent(shareId)),
                        }).done(function() {
-                               self.fetch();
+                               self.fetch({
+                                       success: function() {
+                                               if (_.isFunction(options.success)) {
+                                                       options.success(self);
+                                               }
+                                       }
+                               });
+                       }).fail(function(xhr) {
+                               var msg = t('core', 'Error');
+                               var result = xhr.responseJSON;
+                               if (result.ocs && result.ocs.meta) {
+                                       msg = result.ocs.meta.message;
+                               }
+
+                               if (_.isFunction(options.error)) {
+                                       options.error(self, msg);
+                               } else {
+                                       OC.dialogs.alert(msg, t('core', 'Error removing share'));
+                               }
                        });
                },
 
index e1a14bbbe7fa1951d21f78709d2e4757feeb3a6a..b2480a8beaacdf532b46dc7e22d11b1ace06a88d 100644 (file)
@@ -659,6 +659,47 @@ describe('OC.Share.ShareItemModel', function() {
                                password: 'test'
                        });
                });
+               it('forwards error message on add', function() {
+                       var errorStub = sinon.stub();
+                       model.set({
+                               linkShare: {
+                                       isLinkShare: false
+                               }
+                       }, {
+                       });
+
+                       model.saveLinkShare({
+                               password: 'test'
+                       }, {
+                               error: errorStub
+                       });
+
+                       addShareStub.yieldTo('error', 'Some error message');
+
+                       expect(errorStub.calledOnce).toEqual(true);
+                       expect(errorStub.lastCall.args[0]).toEqual('Some error message');
+               });
+               it('forwards error message on update', function() {
+                       var errorStub = sinon.stub();
+                       model.set({
+                               linkShare: {
+                                       isLinkShare: true,
+                                       id: '123'
+                               }
+                       }, {
+                       });
+
+                       model.saveLinkShare({
+                               password: 'test'
+                       }, {
+                               error: errorStub
+                       });
+
+                       updateShareStub.yieldTo('error', 'Some error message');
+
+                       expect(errorStub.calledOnce).toEqual(true);
+                       expect(errorStub.lastCall.args[0]).toEqual('Some error message');
+               });
        });
        describe('creating shares', function() {
                it('sends POST method to endpoint with passed values', function() {
@@ -680,6 +721,31 @@ describe('OC.Share.ShareItemModel', function() {
                                shareWith: 'group1'
                        });
                });
+               it('calls error handler with error message', function() {
+                       var errorStub = sinon.stub();
+                       model.addShare({
+                               shareType: OC.Share.SHARE_TYPE_GROUP,
+                               shareWith: 'group1'
+                       }, {
+                               error: errorStub
+                       });
+
+                       expect(fakeServer.requests.length).toEqual(1);
+                       fakeServer.requests[0].respond(
+                               400,
+                               { 'Content-Type': 'application/json' },
+                               JSON.stringify({
+                                       ocs: {
+                                               meta: {
+                                                       message: 'Some error message'
+                                               }
+                                       }
+                               })
+                       );
+
+                       expect(errorStub.calledOnce).toEqual(true);
+                       expect(errorStub.lastCall.args[1]).toEqual('Some error message');
+               });
        });
        describe('updating shares', function() {
                it('sends PUT method to endpoint with passed values', function() {
@@ -697,6 +763,30 @@ describe('OC.Share.ShareItemModel', function() {
                                permissions: '' + (OC.PERMISSION_READ | OC.PERMISSION_SHARE)
                        });
                });
+               it('calls error handler with error message', function() {
+                       var errorStub = sinon.stub();
+                       model.updateShare(123, {
+                               permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE
+                       }, {
+                               error: errorStub
+                       });
+
+                       expect(fakeServer.requests.length).toEqual(1);
+                       fakeServer.requests[0].respond(
+                               400,
+                               { 'Content-Type': 'application/json' },
+                               JSON.stringify({
+                                       ocs: {
+                                               meta: {
+                                                       message: 'Some error message'
+                                               }
+                                       }
+                               })
+                       );
+
+                       expect(errorStub.calledOnce).toEqual(true);
+                       expect(errorStub.lastCall.args[1]).toEqual('Some error message');
+               });
        });
        describe('removing shares', function() {
                it('sends DELETE method to endpoint with share id', function() {
@@ -709,6 +799,28 @@ describe('OC.Share.ShareItemModel', function() {
                                'shares/123?format=json'
                        );
                });
+               it('calls error handler with error message', function() {
+                       var errorStub = sinon.stub();
+                       model.removeShare(123, {
+                               error: errorStub
+                       });
+
+                       expect(fakeServer.requests.length).toEqual(1);
+                       fakeServer.requests[0].respond(
+                               400,
+                               { 'Content-Type': 'application/json' },
+                               JSON.stringify({
+                                       ocs: {
+                                               meta: {
+                                                       message: 'Some error message'
+                                               }
+                                       }
+                               })
+                       );
+
+                       expect(errorStub.calledOnce).toEqual(true);
+                       expect(errorStub.lastCall.args[1]).toEqual('Some error message');
+               });
        });
 });