use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class ErrorResponseBuilder method buildRestLiResponseData.
@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object object, Map<String, String> headers, List<HttpCookie> cookies) {
RestLiServiceException exceptionResult = (RestLiServiceException) object;
if (_errorResponseFormat.showHeaders()) {
final ProtocolVersion protocolVersion = ProtocolVersionUtil.extractProtocolVersion(headers);
headers.put(HeaderUtil.getErrorResponseHeaderName(protocolVersion), RestConstants.HEADER_VALUE_ERROR);
}
final ResourceMethod type;
if (routingResult != null && routingResult.getResourceMethod() != null) {
type = routingResult.getResourceMethod().getMethodType();
} else {
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(exceptionResult, headers, cookies);
return responseData;
}
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(exceptionResult, headers, cookies);
responseData.setResponseEnvelope(EnvelopeBuilderUtil.buildBlankResponseEnvelope(type, responseData));
return responseData;
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class AlbumResource method update.
// update an existing photo with given entity
@Override
public UpdateResponse update(Long key, Album entity) {
final Album currPhoto = _db.getData().get(key);
if (currPhoto == null) {
return new UpdateResponse(HttpStatus.S_404_NOT_FOUND);
}
// disallow changing entity ID and URN
if (entity.hasId() || entity.hasUrn()) {
throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Album ID is not acceptable in request");
}
// make sure the ID in the entity is consistent with the key in the database
entity.setId(key);
entity.setUrn(String.valueOf(key));
_db.getData().put(key, entity);
return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class PhotoResource method update.
// update an existing photo with given entity
@Override
public UpdateResponse update(Long key, Photo entity) {
final Photo currPhoto = _db.getData().get(key);
if (currPhoto == null) {
return new UpdateResponse(HttpStatus.S_404_NOT_FOUND);
}
//ID and URN are required fields, so use a dummy value to denote "empty" fields
if ((entity.hasId() && entity.getId() != -1) || (entity.hasUrn() && !entity.getUrn().equals(""))) {
throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Photo ID is not acceptable in request");
}
// make sure the ID in the entity is consistent with the key in the database
entity.setId(key);
entity.setUrn(String.valueOf(key));
_db.getData().put(key, entity);
return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class PhotoResource method batchGet.
@Override
public BatchResult<Long, Photo> batchGet(Set<Long> ids) {
Map<Long, Photo> result = new HashMap<Long, Photo>();
Map<Long, RestLiServiceException> errors = new HashMap<Long, RestLiServiceException>();
for (Long key : ids) {
if (get(key) != null) {
result.put(key, get(key));
} else {
errors.put(key, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "No photo with id=" + key + " has been found."));
}
}
return new BatchResult<Long, Photo>(result, errors);
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class TestRestLiResponseHandler method testRestErrors.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
void testRestErrors(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
RestResponse response;
RestLiServiceException ex;
final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
// #1
ex = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "missing fields");
response = _responseHandler.buildResponse(request, buildRoutingResult(request, acceptTypeData.acceptHeaders), ex);
checkResponse(response, 400, 3, acceptTypeData.responseContentType, ErrorResponse.class.getName(), null, true, true, errorResponseHeaderName);
DataMap dataMap = acceptTypeData.dataCodec.readMap(response.getEntity().asInputStream());
assertEquals(dataMap.getInteger("status"), Integer.valueOf(400));
assertEquals(dataMap.getString("message"), "missing fields");
// #2
ex = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "missing fields").setServiceErrorCode(11);
response = _responseHandler.buildResponse(request, buildRoutingResult(request, acceptTypeData.acceptHeaders), ex);
checkResponse(response, 400, 3, acceptTypeData.responseContentType, ErrorResponse.class.getName(), null, true, true, errorResponseHeaderName);
dataMap = acceptTypeData.dataCodec.readMap(response.getEntity().asInputStream());
assertEquals(dataMap.getInteger("status"), Integer.valueOf(400));
assertEquals(dataMap.getString("message"), "missing fields");
assertEquals(dataMap.getInteger("serviceErrorCode"), Integer.valueOf(11));
}
Aggregations