Return 422 if entity from request body cannot be processed

If a group/project/account which is referenced in the body of a request
cannot be found or is not processable the response code should be '422
Unprocessable Entity'. At the moment we are throwing '404 Not Found' or
'400 Bad Request'. '404 Not Found' is wrong since this status code must
only be used if the resource from the URL is not found. '400 Bad
Request' would be okay, but should rather be used if the request body
is not parseable.

Change-Id: Idcfff67c81eac2e3ea19d73078d2a7ff599d7d02
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2013-03-08 15:42:05 +01:00
parent d54de1cbb6
commit 7abdd70b76
15 changed files with 179 additions and 226 deletions

View File

@@ -22,6 +22,7 @@ import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestCollection;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.extensions.restapi.TopLevelResource;
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.AnonymousUser;
import com.google.gerrit.server.CurrentUser;
@@ -56,11 +57,35 @@ public class AccountsCollection implements
@Override
public AccountResource parse(TopLevelResource root, IdString id)
throws ResourceNotFoundException, AuthException, OrmException {
return new AccountResource(parse(id.get()));
IdentifiedUser user = _parse(id.get());
if (user == null) {
throw new ResourceNotFoundException(id);
}
return new AccountResource(user);
}
/**
* Parses a account ID from a request body and returns the user.
*
* @param id ID of the account, can be a string of the format
* "Full Name <email@example.com>", just the email address, a full name
* if it is unique, an account ID, a user name or 'self' for the
* calling user
* @return the project
* @throws UnprocessableEntityException thrown if the account ID cannot be
* resolved or if the account is not visible to the calling user
*/
public IdentifiedUser parse(String id) throws AuthException,
ResourceNotFoundException, OrmException {
UnprocessableEntityException, OrmException {
IdentifiedUser user = _parse(id);
if (user == null) {
throw new UnprocessableEntityException(String.format(
"Account Not Found: %s", id));
}
return user;
}
private IdentifiedUser _parse(String id) throws AuthException, OrmException {
CurrentUser user = self.get();
if (id.equals("self")) {
@@ -69,13 +94,13 @@ public class AccountsCollection implements
} else if (user instanceof AnonymousUser) {
throw new AuthException("Authentication required");
} else {
throw new ResourceNotFoundException(id);
return null;
}
}
Set<Account.Id> matches = resolver.findAll(id);
if (matches.size() != 1) {
throw new ResourceNotFoundException(id);
return null;
}
Account.Id a = Iterables.getOnlyElement(matches);
@@ -83,7 +108,7 @@ public class AccountsCollection implements
|| user.getCapabilities().canAdministrateServer()) {
return userFactory.create(a);
} else {
throw new ResourceNotFoundException(id);
return null;
}
}