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:
@@ -21,6 +21,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.Project;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.OutputFormat;
|
||||
@@ -56,21 +57,41 @@ public class ProjectsCollection implements
|
||||
@Override
|
||||
public ProjectResource parse(TopLevelResource parent, IdString id)
|
||||
throws ResourceNotFoundException {
|
||||
return parse(id.get());
|
||||
ProjectResource rsrc = _parse(id.get());
|
||||
if (rsrc == null) {
|
||||
throw new ResourceNotFoundException(id);
|
||||
}
|
||||
return rsrc;
|
||||
}
|
||||
|
||||
public ProjectResource parse(String id)
|
||||
throws ResourceNotFoundException {
|
||||
/**
|
||||
* Parses a project ID from a request body and returns the project.
|
||||
*
|
||||
* @param id ID of the project, can be a project name
|
||||
* @return the project
|
||||
* @throws UnprocessableEntityException thrown if the project ID cannot be
|
||||
* resolved or if the project is not visible to the calling user
|
||||
*/
|
||||
public ProjectResource parse(String id) throws UnprocessableEntityException {
|
||||
ProjectResource rsrc = _parse(id);
|
||||
if (rsrc == null) {
|
||||
throw new UnprocessableEntityException(String.format(
|
||||
"Project Not Found: %s", id));
|
||||
}
|
||||
return rsrc;
|
||||
}
|
||||
|
||||
private ProjectResource _parse(String id) {
|
||||
ProjectControl ctl;
|
||||
try {
|
||||
ctl = controlFactory.controlFor(
|
||||
new Project.NameKey(id),
|
||||
user.get());
|
||||
} catch (NoSuchProjectException e) {
|
||||
throw new ResourceNotFoundException(id);
|
||||
return null;
|
||||
}
|
||||
if (!ctl.isVisible() && !ctl.isOwner()) {
|
||||
throw new ResourceNotFoundException(id);
|
||||
return null;
|
||||
}
|
||||
return new ProjectResource(ctl);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user