diff --git a/src/javascripts/ng-admin/Crud/delete/DeleteController.js b/src/javascripts/ng-admin/Crud/delete/DeleteController.js index e2353473..35abefd8 100644 --- a/src/javascripts/ng-admin/Crud/delete/DeleteController.js +++ b/src/javascripts/ng-admin/Crud/delete/DeleteController.js @@ -1,9 +1,10 @@ export default class DeleteController { - constructor($scope, $window, $state, $q, WriteQueries, notification, params, view, entry) { + constructor($scope, $window, $state, $q, WriteQueries, Configuration, notification, params, view, entry) { this.$scope = $scope; this.$window = $window; this.$state = $state; this.WriteQueries = WriteQueries; + this.config = Configuration(); this.entityLabel = params.entity; this.entityId = params.id; this.view = view; @@ -42,17 +43,12 @@ export default class DeleteController { notification.log('Element successfully deleted.', { addnCls: 'humane-flatty-success' }); }); - }, - response => { - // @TODO: share this method when splitting controllers - var body = response.data; - if (typeof body === 'object') { - body = JSON.stringify(body); - } - - notification.log('Oops, an error occured : (code: ' + response.status + ') ' + body, {addnCls: 'humane-flatty-error'}); } - ); + ) + .catch(error => { + const errorMessage = this.config.getErrorMessageFor(this.view, error); + notification.log(errorMessage, {addnCls: 'humane-flatty-error'}); + }); } back() { @@ -67,4 +63,4 @@ export default class DeleteController { } } -DeleteController.$inject = ['$scope', '$window', '$state', '$q', 'WriteQueries', 'notification', 'params', 'view', 'entry']; +DeleteController.$inject = ['$scope', '$window', '$state', '$q', 'WriteQueries', 'NgAdminConfiguration', 'notification', 'params', 'view', 'entry']; diff --git a/src/javascripts/test/unit/Crud/delete/DeleteControllerSpec.js b/src/javascripts/test/unit/Crud/delete/DeleteControllerSpec.js index 357a5305..c5689a6e 100644 --- a/src/javascripts/test/unit/Crud/delete/DeleteControllerSpec.js +++ b/src/javascripts/test/unit/Crud/delete/DeleteControllerSpec.js @@ -5,7 +5,7 @@ describe('DeleteController', function () { Entity = require('admin-config/lib/Entity/Entity'), humane = require('humane-js'); - var $scope, $window, $state, $q, writeQueries, notification, params, view, entry; + var $scope, $window, $state, $q, writeQueries, Configuration, notification, params, view, entry; beforeEach(inject(function ($controller, $rootScope, _$window_, _$q_) { $scope = $rootScope.$new(); $window = _$window_; @@ -18,6 +18,11 @@ describe('DeleteController', function () { writeQueries = { deleteOne: jasmine.createSpy('writeQueries.deleteOne').and.callFake(() => $q.when()) }; + Configuration = () => ({ + getErrorMessageFor: function (view, response) { + return 'An error occured: ' + response; + } + }); notification = humane; params = { id: 3, @@ -45,7 +50,7 @@ describe('DeleteController', function () { getEntity: () => entity }; - let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, notification, { + let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, Configuration, notification, { id: deletedId, entity: 'post' }, view, entry); @@ -72,7 +77,7 @@ describe('DeleteController', function () { getEntity: () => entity }; - let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, notification, { + let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, Configuration, notification, { id: deletedId, entity: 'post' }, view, entry); @@ -103,7 +108,7 @@ describe('DeleteController', function () { }; let $window = { history: { back: jasmine.createSpy('$window.history.back') } }; - let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, notification, { + let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, Configuration, notification, { id: commentId, entity: 'comment' }, view, entry);