Merge "Improve error message of when Conflict error occur in ng-swift"

This commit is contained in:
Jenkins 2016-04-21 15:14:43 +00:00 committed by Gerrit Code Review
commit b3e0ddcb75
3 changed files with 104 additions and 31 deletions

View File

@ -93,7 +93,11 @@ class Container(generic.View):
@rest_utils.ajax()
def delete(self, request, container):
api.swift.swift_delete_container(request, container)
try:
api.swift.swift_delete_container(request, container)
except exceptions.Conflict as e:
# It cannot be deleted if it's not empty.
return rest_utils.JSONResponse(str(e), 409)
@rest_utils.ajax(data_required=True)
def put(self, request, container):
@ -172,19 +176,23 @@ class Object(generic.View):
data = form.clean()
if object_name[-1] == '/':
result = api.swift.swift_create_pseudo_folder(
request,
container,
object_name
)
else:
result = api.swift.swift_upload_object(
request,
container,
object_name,
data['file']
)
try:
if object_name[-1] == '/':
result = api.swift.swift_create_pseudo_folder(
request,
container,
object_name
)
else:
result = api.swift.swift_upload_object(
request,
container,
object_name,
data['file']
)
except exceptions.AlreadyExists as e:
# 409 Conflict
return rest_utils.JSONResponse(str(e), 409)
return rest_utils.CreatedResponse(
u'/api/swift/containers/%s/object/%s' % (container, result.name)
@ -192,7 +200,12 @@ class Object(generic.View):
@rest_utils.ajax()
def delete(self, request, container, object_name):
api.swift.swift_delete_object(request, container, object_name)
try:
api.swift.swift_delete_object(request, container, object_name)
except exceptions.Conflict as e:
# In case the given object is pseudo folder
# It cannot be deleted if it's not empty.
return rest_utils.JSONResponse(str(e), 409)
def get(self, request, container, object_name):
"""Get the object contents.
@ -247,13 +260,16 @@ class ObjectCopy(generic.View):
def post(self, request, container, object_name):
dest_container = request.DATA['dest_container']
dest_name = request.DATA['dest_name']
result = api.swift.swift_copy_object(
request,
container,
object_name,
dest_container,
dest_name
)
try:
result = api.swift.swift_copy_object(
request,
container,
object_name,
dest_container,
dest_name
)
except exceptions.AlreadyExists as e:
return rest_utils.JSONResponse(str(e), 409)
return rest_utils.CreatedResponse(
u'/api/swift/containers/%s/object/%s' % (dest_container,
result.name)

View File

@ -161,8 +161,12 @@
*/
function deleteContainer(container) {
return apiService.delete(service.getContainerURL(container) + '/metadata/')
.error(function () {
toastService.add('error', gettext('Unable to delete the container.'));
.error(function (response, status) {
if (status === 409) {
toastService.add('error', response);
} else {
toastService.add('error', gettext('Unable to delete the container.'));
}
});
}
@ -233,8 +237,12 @@
}
}
)
.error(function () {
toastService.add('error', gettext('Unable to upload the object.'));
.error(function (response, status) {
if (status === 409) {
toastService.add('error', response);
} else {
toastService.add('error', gettext('Unable to upload the object.'));
}
});
}
@ -294,8 +302,12 @@
service.getObjectURL(container, folderName) + '/',
{}
)
.error(function () {
toastService.add('error', gettext('Unable to create the folder.'));
.error(function (response, status) {
if (status === 409) {
toastService.add('error', response);
} else {
toastService.add('error', gettext('Unable to create the folder.'));
}
});
}
@ -315,8 +327,12 @@
service.getObjectURL(container, objectName, 'copy'),
{dest_container: destContainer, dest_name: destName}
)
.error(function () {
toastService.add('error', gettext('Unable to copy the object.'));
.error(function (response, status) {
if (status === 409) {
toastService.add('error', response);
} else {
toastService.add('error', gettext('Unable to copy the object.'));
}
});
}
}

View File

@ -170,7 +170,48 @@
});
});
it('returns a better error message when delete is prevented', function test() {
it('returns a relevant error message when createFolder returns a 409 error', function test() {
var promise = {error: angular.noop};
spyOn(apiService, 'post').and.returnValue(promise);
spyOn(promise, 'error');
service.createFolder('spam', 'ham');
spyOn(toastService, 'add');
var innerFunc = promise.error.calls.argsFor(0)[0];
// In the case of 409
var message = 'A pseudo-folder with the name "ham" already exists.';
innerFunc(message, 409);
expect(toastService.add).toHaveBeenCalledWith('error', message);
});
it('returns a relevant error message when uploadObject returns a 409 error', function test() {
var promise = {error: angular.noop};
spyOn(apiService, 'post').and.returnValue(promise);
spyOn(promise, 'error');
service.uploadObject('spam', 'ham');
spyOn(toastService, 'add');
var innerFunc = promise.error.calls.argsFor(0)[0];
// In the case of 409
var message = 'A object with the name "ham" already exists.';
innerFunc(message, 409);
expect(toastService.add).toHaveBeenCalledWith('error', message);
});
it('returns a relevant error message when deleteContainer returns a 409 error',
function test() {
var promise = {error: angular.noop};
spyOn(apiService, 'delete').and.returnValue(promise);
spyOn(promise, 'error');
service.deleteContainer('spam', 'ham');
spyOn(toastService, 'add');
var innerFunc = promise.error.calls.argsFor(0)[0];
// In the case of 409
var message = 'Unable to delete the container because it is not empty.';
innerFunc(message, 409);
expect(toastService.add).toHaveBeenCalledWith('error', message);
}
);
it('returns a relevant error message when deleteObject returns a 409 error', function test() {
var promise = {error: angular.noop};
spyOn(apiService, 'delete').and.returnValue(promise);
spyOn(promise, 'error');