From c4a27b2c7afd8186bbb5931dbaa1e99ad0a2bd8e Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Mon, 18 Aug 2014 18:21:53 -0400 Subject: [PATCH] Add a unified error display method to make the code cleaner. Also fixes a UI issue in create org --- static/js/app.js | 165 +++++++++----------------- static/js/controllers.js | 160 +++++-------------------- static/partials/new-organization.html | 2 +- static/partials/repo-admin.html | 38 ------ 4 files changed, 86 insertions(+), 279 deletions(-) diff --git a/static/js/app.js b/static/js/app.js index 5a5994416..ad6527fc1 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -59,18 +59,8 @@ function getFirstTextLine(commentString) { } function createRobotAccount(ApiService, is_org, orgname, name, callback) { - ApiService.createRobot(is_org ? orgname : null, null, {'robot_shortname': name}).then(callback, function(resp) { - bootbox.dialog({ - "message": resp.data ? resp.data['message'] : 'The robot account could not be created', - "title": "Cannot create robot account", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + ApiService.createRobot(is_org ? orgname : null, null, {'robot_shortname': name}) + .then(callback, ApiService.errorDisplay('Cannot create robot account')); } function createOrganizationTeam(ApiService, orgname, teamname, callback) { @@ -84,18 +74,8 @@ function createOrganizationTeam(ApiService, orgname, teamname, callback) { 'teamname': teamname }; - ApiService.updateOrganizationTeam(data, params).then(callback, function(resp) { - bootbox.dialog({ - "message": resp.data ? resp.data : 'The team could not be created', - "title": "Cannot create team", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + ApiService.updateOrganizationTeam(data, params) + .then(callback, ApiService.errorDisplay('Cannot create team')); } function getMarkedDown(string) { @@ -847,6 +827,38 @@ quayApp = angular.module('quay', quayDependencies, function($provide, cfpLoading buildMethodsForEndpointResource(endpointResource, resourceMap); } + apiService.getErrorMessage = function(resp, defaultMessage) { + var message = defaultMessage; + if (resp['data']) { + message = resp['data']['error_message'] || resp['data']['message'] || resp['data']['error_description'] || message; + } + + return message; + }; + + apiService.errorDisplay = function(defaultMessage, opt_handler) { + return function(resp) { + var message = apiService.getErrorMessage(resp, defaultMessage); + if (opt_handler) { + var handlerMessage = opt_handler(resp); + if (handlerMessage) { + message = handlerMessage; + } + } + + bootbox.dialog({ + "message": message, + "title": defaultMessage, + "buttons": { + "close": { + "label": "Close", + "className": "btn-primary" + } + } + }); + }; + }; + return apiService; }]); @@ -2013,18 +2025,7 @@ quayApp.directive('applicationReference', function () { template: '/static/directives/application-reference-dialog.html', show: true }); - }, function() { - bootbox.dialog({ - "message": 'The application could not be found; it might have been deleted.', - "title": "Cannot find application", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Application could not be found')); }; } }; @@ -2793,18 +2794,7 @@ quayApp.directive('applicationManager', function () { ApiService.createOrganizationApplication(data, params).then(function(resp) { $scope.applications.push(resp); - }, function(resp) { - bootbox.dialog({ - "message": resp['message'] || 'The application could not be created', - "title": "Cannot create application", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Cannot create application')); }; var update = function() { @@ -2889,18 +2879,7 @@ quayApp.directive('robotsManager', function () { if (index >= 0) { $scope.robots.splice(index, 1); } - }, function() { - bootbox.dialog({ - "message": 'The selected robot account could not be deleted', - "title": "Cannot delete robot account", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Cannot delete robot account')); }; var update = function() { @@ -2965,18 +2944,7 @@ quayApp.directive('prototypeManager', function () { ApiService.updateOrganizationPrototypePermission(data, params).then(function(resp) { prototype.role = role; - }, function(resp) { - bootbox.dialog({ - "message": resp.data ? resp.data : 'The permission could not be modified', - "title": "Cannot modify permission", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Cannot modify permission')); }; $scope.comparePrototypes = function(p) { @@ -3016,23 +2984,16 @@ quayApp.directive('prototypeManager', function () { data['activating_user'] = $scope.activatingForNew; } + var errorHandler = ApiService.errorDisplay('Cannot create permission', + function(resp) { + $('#addPermissionDialogModal').modal('hide'); + }); + ApiService.createOrganizationPrototypePermission(data, params).then(function(resp) { $scope.prototypes.push(resp); $scope.loading = false; $('#addPermissionDialogModal').modal('hide'); - }, function(resp) { - $('#addPermissionDialogModal').modal('hide'); - bootbox.dialog({ - "message": resp.data ? resp.data : 'The permission could not be created', - "title": "Cannot create permission", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, errorHandler); }; $scope.deletePrototype = function(prototype) { @@ -3046,18 +3007,7 @@ quayApp.directive('prototypeManager', function () { ApiService.deleteOrganizationPrototypePermission(null, params).then(function(resp) { $scope.prototypes.splice($scope.prototypes.indexOf(prototype), 1); $scope.loading = false; - }, function(resp) { - bootbox.dialog({ - "message": resp.data ? resp.data : 'The permission could not be deleted', - "title": "Cannot delete permission", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Cannot delete permission')); }; var update = function() { @@ -4377,26 +4327,17 @@ quayApp.directive('setupTriggerDialog', function () { $scope.activating = true; + var errorHandler = ApiService.errorDisplay('Cannot activate build trigger', function(resp) { + $scope.hide(); + $scope.canceled({'trigger': $scope.trigger}); + }); + ApiService.activateBuildTrigger(data, params).then(function(resp) { $scope.hide(); $scope.trigger['is_active'] = true; $scope.trigger['pull_robot'] = resp['pull_robot']; $scope.activated({'trigger': $scope.trigger}); - }, function(resp) { - $scope.hide(); - $scope.canceled({'trigger': $scope.trigger}); - - bootbox.dialog({ - "message": resp['data']['message'] || 'The build trigger setup could not be completed', - "title": "Could not activate build trigger", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, errorHandler); }; var check = function() { diff --git a/static/js/controllers.js b/static/js/controllers.js index e04dd0a3c..aa18c1b40 100644 --- a/static/js/controllers.js +++ b/static/js/controllers.js @@ -532,23 +532,15 @@ function RepoCtrl($scope, $sanitize, Restangular, ImageMetadataService, ApiServi 'image': image.id }; + var errorHandler = ApiService.errorDisplay('Cannot create or move tag', function(resp) { + $('#addTagModal').modal('hide'); + }); + ApiService.changeTagImage(data, params).then(function(resp) { $scope.creatingTag = false; loadViewInfo(); $('#addTagModal').modal('hide'); - }, function(resp) { - $('#addTagModal').modal('hide'); - bootbox.dialog({ - "message": resp.data ? resp.data : 'Could not create or move tag', - "title": "Cannot create or move tag", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, errorHandler); }; $scope.deleteTag = function(tagName) { @@ -562,18 +554,7 @@ function RepoCtrl($scope, $sanitize, Restangular, ImageMetadataService, ApiServi ApiService.deleteFullTag(null, params).then(function() { loadViewInfo(); - }, function(resp) { - bootbox.dialog({ - "message": resp.data ? resp.data : 'Could not delete tag', - "title": "Cannot delete tag", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Cannot delete tag')); }; $scope.getImagesForTagBySize = function(tag) { @@ -1362,17 +1343,16 @@ function RepoAdminCtrl($scope, Restangular, ApiService, KeyService, $routeParams }; $scope.deleteRole = function(entityName, kind) { + var errorHandler = ApiService.errorDisplay('Cannot change permission', function(resp) { + if (resp.status == 409) { + return 'Cannot change permission as you do not have the authority'; + } + }); + var permissionDelete = Restangular.one(getRestUrl('repository', namespace, name, 'permissions', kind, entityName)); permissionDelete.customDELETE().then(function() { delete $scope.permissions[kind][entityName]; - }, function(resp) { - if (resp.status == 409) { - $scope.changePermError = resp.data || ''; - $('#channgechangepermModal').modal({}); - } else { - $('#cannotchangeModal').modal({}); - } - }); + }, errorHandler); }; $scope.addRole = function(entityName, role, kind) { @@ -1383,9 +1363,7 @@ function RepoAdminCtrl($scope, Restangular, ApiService, KeyService, $routeParams var permissionPost = Restangular.one(getRestUrl('repository', namespace, name, 'permissions', kind, entityName)); permissionPost.customPUT(permission).then(function(result) { $scope.permissions[kind][entityName] = result; - }, function(result) { - $('#cannotchangeModal').modal({}); - }); + }, ApiService.errorDisplay('Cannot change permission')); }; $scope.roles = [ @@ -1600,18 +1578,7 @@ function RepoAdminCtrl($scope, Restangular, ApiService, KeyService, $routeParams window.console.log(resp); var url = '/repository/' + namespace + '/' + name + '/build?current=' + resp['id']; document.location = url; - }, function(resp) { - bootbox.dialog({ - "message": resp['message'] || 'The build could not be started', - "title": "Could not start build", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Could not start build')); }; $scope.deleteTrigger = function(trigger) { @@ -1739,18 +1706,7 @@ function UserAdminCtrl($scope, $timeout, $location, ApiService, PlanService, Use ApiService.deleteUserAuthorization(null, params).then(function(resp) { $scope.authorizedApps.splice($scope.authorizedApps.indexOf(accessTokenInfo), 1); - }, function(resp) { - bootbox.dialog({ - "message": resp.message || 'Could not revoke authorization', - "title": "Cannot revoke authorization", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Could not revoke authorization')); }; $scope.loadLogs = function() { @@ -2215,13 +2171,14 @@ function OrgViewCtrl($rootScope, $scope, ApiService, $routeParams) { 'teamname': teamname }; + var errorHandler = ApiService.errorDisplay('Cannot delete team', function() { + $scope.currentDeleteTeam = null; + }); + ApiService.deleteOrganizationTeam(null, params).then(function() { delete $scope.organization.teams[teamname]; $scope.currentDeleteTeam = null; - }, function() { - $('#cannotchangeModal').modal({}); - $scope.currentDeleteTeam = null; - }); + }, errorHandler); }; var loadOrganization = function() { @@ -2515,9 +2472,9 @@ function NewOrgCtrl($scope, $routeParams, $timeout, $location, UserService, Plan }; PlanService.changePlan($scope, org.name, $scope.holder.currentPlan.stripeId, callbacks); - }, function(result) { + }, function(resp) { $scope.creating = false; - $scope.createError = result.data.error_description || result.data; + $scope.createError = ApiService.getErrorMessage(resp); $timeout(function() { $('#orgName').popover('show'); }); @@ -2594,18 +2551,7 @@ function ManageApplicationCtrl($scope, $routeParams, $rootScope, $location, $tim $timeout(function() { $location.path('/organization/' + orgname + '/admin'); }, 500); - }, function(resp) { - bootbox.dialog({ - "message": resp.message || 'Could not delete application', - "title": "Cannot delete application", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Could not delete application')); }; $scope.updateApplication = function() { @@ -2623,22 +2569,13 @@ function ManageApplicationCtrl($scope, $routeParams, $rootScope, $location, $tim delete $scope.application['gravatar_email']; } + var errorHandler = ApiService.errorDisplay('Could not update application', function(resp) { + $scope.updating = false; + }); + ApiService.updateOrganizationApplication($scope.application, params).then(function(resp) { $scope.application = resp; - $scope.updating = false; - }, function(resp) { - $scope.updating = false; - bootbox.dialog({ - "message": resp.message || 'Could not update application', - "title": "Cannot update application", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, errorHandler); }; $scope.resetClientSecret = function() { @@ -2651,18 +2588,7 @@ function ManageApplicationCtrl($scope, $routeParams, $rootScope, $location, $tim ApiService.resetOrganizationApplicationClientSecret(null, params).then(function(resp) { $scope.application = resp; - }, function(resp) { - bootbox.dialog({ - "message": resp.message || 'Could not reset client secret', - "title": "Cannot reset client secret", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Could not reset client secret')); }; var loadOrganization = function() { @@ -2758,18 +2684,7 @@ function SuperUserAdminCtrl($scope, ApiService, Features, UserService) { ApiService.changeInstallUser(data, params).then(function(resp) { $scope.loadUsersInternal(); - }, function(resp) { - bootbox.dialog({ - "message": resp.data ? resp.data.message : 'Could not change user', - "title": "Cannot change user", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Could not change user')); }; $scope.deleteUser = function(user) { @@ -2781,18 +2696,7 @@ function SuperUserAdminCtrl($scope, ApiService, Features, UserService) { ApiService.deleteInstallUser(null, params).then(function(resp) { $scope.loadUsersInternal(); - }, function(resp) { - bootbox.dialog({ - "message": resp.data ? resp.data.message : 'Could not delete user', - "title": "Cannot delete user", - "buttons": { - "close": { - "label": "Close", - "className": "btn-primary" - } - } - }); - }); + }, ApiService.errorDisplay('Cannot delete user')); }; var seatUsageLoaded = function(usage) { diff --git a/static/partials/new-organization.html b/static/partials/new-organization.html index 3ba81e155..0de90293f 100644 --- a/static/partials/new-organization.html +++ b/static/partials/new-organization.html @@ -53,7 +53,7 @@ + data-placement="bottom" data-container="body" ng-pattern="/^[a-z0-9_]{4,30}$/"> This will also be the namespace for your repositories diff --git a/static/partials/repo-admin.html b/static/partials/repo-admin.html index 17405f7ff..b3ef4b51b 100644 --- a/static/partials/repo-admin.html +++ b/static/partials/repo-admin.html @@ -377,24 +377,6 @@ counter="showNewNotificationCounter" notification-created="handleNotificationCreated(notification)"> - - -