use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class RestLiInternalException method toErrorResponse.
public ErrorResponse toErrorResponse() {
ErrorResponse response = new ErrorResponse();
response.setStatus(RestStatus.INTERNAL_SERVER_ERROR);
response.setExceptionClass(RestLiInternalException.class.getName());
if (getMessage() != null) {
response.setMessage(getMessage());
}
return response;
}
use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class TestBatchGetResponseBuilder method testBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "testData")
@SuppressWarnings("unchecked")
public void testBuilder(Object results, ProtocolVersion protocolVersion, Map<String, Foo> expectedTransformedResult, Map<String, ErrorResponse> expectedErrors, Map<Object, RestLiServiceException> expectedExceptionsWithUntypedKey, MaskTree maskTree, ProjectionMode projectionMode) {
ResourceContext mockContext = getMockResourceContext(protocolVersion, expectedExceptionsWithUntypedKey, null, maskTree, projectionMode);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
BatchGetResponseBuilder responseBuilder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
PartialRestResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
EasyMock.verify(mockContext, mockDescriptor);
ResponseBuilderUtil.validateHeaders(restResponse, headers);
Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
BatchResponse<Foo> entity = (BatchResponse<Foo>) restResponse.getEntity();
Assert.assertEquals(entity.getResults(), expectedTransformedResult);
if (results instanceof BatchResult) {
Map<String, Integer> expectedStatuses = new HashMap<String, Integer>();
for (String key : entity.getResults().keySet()) {
expectedStatuses.put(key, HttpStatus.S_200_OK.getCode());
}
Assert.assertEquals(entity.getStatuses(), expectedStatuses);
} else {
// if the resource returns a Map we don't have a separate status map in the BatchResponse
Assert.assertEquals(entity.getStatuses(), Collections.emptyMap());
}
Assert.assertEquals(entity.getErrors().size(), expectedErrors.size());
for (Map.Entry<String, ErrorResponse> entry : entity.getErrors().entrySet()) {
String key = entry.getKey();
ErrorResponse value = entry.getValue();
Assert.assertEquals(value.getStatus(), expectedErrors.get(key).getStatus());
}
}
use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class TestBatchUpdateResponseBuilder method dataProvider.
@DataProvider(name = "testData")
public Object[][] dataProvider() {
CompoundKey c1 = new CompoundKey().append("a", "a1").append("b", 1);
CompoundKey c2 = new CompoundKey().append("a", "a2").append("b", 2);
CompoundKey c3 = new CompoundKey().append("a", "a3").append("b", 3);
Map<CompoundKey, UpdateResponse> results = new HashMap<CompoundKey, UpdateResponse>();
results.put(c1, new UpdateResponse(HttpStatus.S_202_ACCEPTED));
results.put(c2, new UpdateResponse(HttpStatus.S_202_ACCEPTED));
RestLiServiceException restLiServiceException = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND);
Map<CompoundKey, RestLiServiceException> errors = Collections.singletonMap(c3, restLiServiceException);
BatchUpdateResult<CompoundKey, Foo> batchUpdateResult = new BatchUpdateResult<CompoundKey, Foo>(results, errors);
Map<CompoundKey, UpdateResponse> keyOverlapResults = new HashMap<CompoundKey, UpdateResponse>();
keyOverlapResults.put(c1, new UpdateResponse(HttpStatus.S_202_ACCEPTED));
keyOverlapResults.put(c2, new UpdateResponse(HttpStatus.S_202_ACCEPTED));
keyOverlapResults.put(c3, new UpdateResponse(HttpStatus.S_404_NOT_FOUND));
BatchUpdateResult<CompoundKey, Foo> keyOverlapBatchUpdateResult = new BatchUpdateResult<CompoundKey, Foo>(keyOverlapResults, errors);
UpdateStatus updateStatus = new UpdateStatus().setStatus(202);
ErrorResponse errorResponse = new ErrorResponse().setStatus(404);
Map<String, UpdateStatus> expectedProtocol1Results = new HashMap<String, UpdateStatus>();
expectedProtocol1Results.put("a=a1&b=1", updateStatus);
expectedProtocol1Results.put("a=a2&b=2", updateStatus);
Map<String, ErrorResponse> expectedProtocol1Errors = new HashMap<String, ErrorResponse>();
expectedProtocol1Errors.put("a=a3&b=3", errorResponse);
Map<String, UpdateStatus> expectedProtocol2Results = new HashMap<String, UpdateStatus>();
expectedProtocol2Results.put("(a:a1,b:1)", updateStatus);
expectedProtocol2Results.put("(a:a2,b:2)", updateStatus);
Map<String, ErrorResponse> expectedProtocol2Errors = new HashMap<String, ErrorResponse>();
expectedProtocol2Errors.put("(a:a3,b:3)", errorResponse);
Map<String, UpdateStatus> expectedAltKeyResults = new HashMap<String, UpdateStatus>();
expectedAltKeyResults.put("aa1xb1", updateStatus);
expectedAltKeyResults.put("aa2xb2", updateStatus);
Map<String, ErrorResponse> expectedAltKeyErrors = new HashMap<String, ErrorResponse>();
expectedAltKeyErrors.put("aa3xb3", errorResponse);
Map<String, AlternativeKey<?, ?>> alternativeKeyMap = new HashMap<String, AlternativeKey<?, ?>>();
alternativeKeyMap.put("alt", new AlternativeKey<String, CompoundKey>(new TestKeyCoercer(), String.class, new StringDataSchema()));
return new Object[][] { { batchUpdateResult, AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), null, null, expectedProtocol1Results, expectedProtocol1Errors }, { batchUpdateResult, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), null, null, expectedProtocol2Results, expectedProtocol2Errors }, { keyOverlapBatchUpdateResult, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), null, null, expectedProtocol2Results, expectedProtocol2Errors }, { batchUpdateResult, AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), "alt", alternativeKeyMap, expectedAltKeyResults, expectedAltKeyErrors }, { batchUpdateResult, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), "alt", alternativeKeyMap, expectedAltKeyResults, expectedAltKeyErrors } };
}
use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class TestErrorResponseBuilder method testBuilder.
@Test(dataProvider = "testData")
public void testBuilder(ProtocolVersion protocolVersion) {
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
Map<String, String> expectedHeaders = new HashMap<String, String>(headers);
expectedHeaders.put(HeaderUtil.getErrorResponseHeaderName(protocolVersion), RestConstants.HEADER_VALUE_ERROR);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor();
RoutingResult routingResult = new RoutingResult(null, mockDescriptor);
RuntimeException runtimeException = new RuntimeException("Internal server error!");
RestLiServiceException serviceException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, runtimeException);
ErrorResponseBuilder errorResponseBuilder = new ErrorResponseBuilder();
RestLiResponseData responseData = errorResponseBuilder.buildRestLiResponseData(null, routingResult, serviceException, headers, Collections.<HttpCookie>emptyList());
PartialRestResponse restResponse = errorResponseBuilder.buildResponse(routingResult, responseData);
EasyMock.verify(mockDescriptor);
ErrorResponse errorResponse = (ErrorResponse) restResponse.getEntity();
Assert.assertEquals(errorResponse.getStatus(), new Integer(500));
Assert.assertTrue(errorResponse.getMessage().contains(runtimeException.getMessage()));
}
use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class TestErrorResponseBuilder method testOverride.
@Test
public void testOverride() {
RestLiServiceException exception = new RestLiServiceException(HttpStatus.S_200_OK, "Some message", new IllegalStateException("Some other message"));
exception.setServiceErrorCode(123);
exception.setErrorDetails(new DataMap());
ErrorResponseBuilder builder = new ErrorResponseBuilder(ErrorResponseFormat.FULL);
ErrorResponse errorResponse = builder.buildErrorResponse(exception);
Assert.assertTrue(errorResponse.hasErrorDetails());
Assert.assertTrue(errorResponse.hasExceptionClass());
Assert.assertTrue(errorResponse.hasStatus());
Assert.assertTrue(errorResponse.hasMessage());
Assert.assertTrue(errorResponse.hasServiceErrorCode());
Assert.assertTrue(errorResponse.hasStackTrace());
exception.setOverridingFormat(ErrorResponseFormat.MESSAGE_AND_SERVICECODE);
errorResponse = builder.buildErrorResponse(exception);
Assert.assertFalse(errorResponse.hasErrorDetails());
Assert.assertFalse(errorResponse.hasExceptionClass());
Assert.assertTrue(errorResponse.hasStatus());
Assert.assertTrue(errorResponse.hasMessage());
Assert.assertTrue(errorResponse.hasServiceErrorCode());
Assert.assertFalse(errorResponse.hasStackTrace());
}
Aggregations