use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class TestDefaultScatterGatherStrategy method testGatherBatchResponse.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocol")
public void testGatherBatchResponse(ProtocolVersion version) {
Map<RequestInfo, Response<BatchResponse<TestRecord>>> successResponses = new HashMap<>();
successResponses.put(new RequestInfo(createBatchGetRequest(1L, 2L), getTargetHostRequestContext(_host1URI)), createBatchResponse(Collections.singleton(1L), Collections.singleton(2L)));
Map<RequestInfo, Throwable> failResponses = new HashMap<>();
failResponses.put(new RequestInfo(createBatchGetRequest(3L), getTargetHostRequestContext(_host2URI)), new RestLiScatterGatherException("Partition host is unavailable!"));
Callback<Response<BatchResponse<TestRecord>>> testCallback = new Callback<Response<BatchResponse<TestRecord>>>() {
@Override
public void onError(Throwable e) {
}
@Override
public void onSuccess(Response<BatchResponse<TestRecord>> result) {
Assert.assertNotNull(result.getEntity());
Assert.assertEquals(result.getStatus(), HttpStatus.S_200_OK.getCode());
Assert.assertTrue(result.getEntity().getResults().size() == 1);
Assert.assertTrue(result.getEntity().getResults().containsKey("1"));
Assert.assertTrue(result.getEntity().getErrors().size() == 3);
ErrorResponse keyError = result.getEntity().getErrors().get("2");
Assert.assertEquals(keyError.getStatus().intValue(), HttpStatus.S_404_NOT_FOUND.getCode());
ErrorResponse failError = result.getEntity().getErrors().get("3");
Assert.assertEquals(failError.getExceptionClass(), RestLiScatterGatherException.class.getName());
Assert.assertEquals(failError.getMessage(), "Partition host is unavailable!");
ErrorResponse unmappedError = result.getEntity().getErrors().get("4");
Assert.assertEquals(unmappedError.getExceptionClass(), RestLiScatterGatherException.class.getName());
Assert.assertEquals(unmappedError.getMessage(), "Unable to find a host for keys :[4]");
}
};
_sgStrategy.onAllResponsesReceived(_batchGetRequest, version, successResponses, failResponses, _unmappedKeys, testCallback);
}
use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class TestDefaultScatterGatherStrategy method createBatchResponse.
private static Response<BatchResponse<TestRecord>> createBatchResponse(Set<Long> resultKeys, Set<Long> errorKeys) {
DataMap resultMap = new DataMap();
for (Long id : resultKeys) {
resultMap.put(id.toString(), new TestRecord().setId(id).data());
}
DataMap errorMap = new DataMap();
for (Long id : errorKeys) {
errorMap.put(id.toString(), new ErrorResponse().setStatus(HttpStatus.S_404_NOT_FOUND.getCode()).data());
}
DataMap responseMap = new DataMap();
responseMap.put(BatchResponse.RESULTS, resultMap);
responseMap.put(BatchResponse.ERRORS, errorMap);
BatchResponse<TestRecord> response = new BatchResponse<>(responseMap, TestRecord.class);
return new ResponseImpl<>(HttpStatus.S_200_OK.getCode(), Collections.emptyMap(), Collections.emptyList(), response, null);
}
use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class RestClientTest method testRestLiResponseFuture.
@SuppressWarnings("deprecation")
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestAndGetResponseOptions")
public void testRestLiResponseFuture(SendRequestOption sendRequestOption, GetResponseOption getResponseOption, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName, ContentType contentType) throws ExecutionException, RemoteInvocationException, TimeoutException, InterruptedException, IOException {
final String ERR_KEY = "someErr";
final String ERR_VALUE = "WHOOPS!";
final String ERR_MSG = "whoops2";
final int HTTP_CODE = 200;
final int APP_CODE = 666;
final String CODE = "INVALID_INPUT";
final String DOC_URL = "https://example.com/errors/invalid-input";
final String REQUEST_ID = "abc123";
RestClient client = mockClient(ERR_KEY, ERR_VALUE, ERR_MSG, HTTP_CODE, APP_CODE, CODE, DOC_URL, REQUEST_ID, protocolVersion, errorResponseHeaderName);
Request<ErrorResponse> request = mockRequest(ErrorResponse.class, versionOption, contentType);
RequestBuilder<Request<ErrorResponse>> requestBuilder = mockRequestBuilder(request);
ResponseFuture<ErrorResponse> future = sendRequest(sendRequestOption, determineErrorHandlingBehavior(getResponseOption), client, request, requestBuilder);
Response<ErrorResponse> response = getOkResponse(getResponseOption, future, timeoutOption);
ErrorResponse e = response.getEntity();
Assert.assertNull(response.getError());
Assert.assertFalse(response.hasError());
Assert.assertEquals(HTTP_CODE, response.getStatus());
Assert.assertEquals(ERR_VALUE, e.getErrorDetails().data().getString(ERR_KEY));
Assert.assertEquals(APP_CODE, e.getServiceErrorCode().intValue());
Assert.assertEquals(ERR_MSG, e.getMessage());
Assert.assertEquals(CODE, e.getCode());
Assert.assertEquals(DOC_URL, e.getDocUrl());
Assert.assertEquals(REQUEST_ID, e.getRequestId());
Assert.assertEquals(EmptyRecord.class.getCanonicalName(), e.getErrorDetailType());
}
use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class RestClientTest method mockClient.
@SuppressWarnings("deprecation")
private RestClient mockClient(String errKey, String errValue, String errMsg, int httpCode, int appCode, String code, String docUrl, String requestId, ProtocolVersion protocolVersion, String errorResponseHeaderName) {
ErrorResponse er = new ErrorResponse();
DataMap errMap = new DataMap();
errMap.put(errKey, errValue);
er.setErrorDetails(new ErrorDetails(errMap));
er.setErrorDetailType(EmptyRecord.class.getCanonicalName());
er.setStatus(httpCode);
er.setMessage(errMsg);
er.setServiceErrorCode(appCode);
er.setCode(code);
er.setDocUrl(docUrl);
er.setRequestId(requestId);
Map<String, String> headers = new HashMap<>();
headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
headers.put(errorResponseHeaderName, RestConstants.HEADER_VALUE_ERROR);
byte[] mapBytes;
try {
mapBytes = DataMapConverter.getContentType(headers).getCodec().mapToBytes(er.data());
} catch (IOException | MimeTypeParseException e) {
throw new RuntimeException(e);
}
return new RestClient(new MyMockClient(httpCode, headers, mapBytes), "http://localhost");
}
use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class IndividualResponseException method createErrorResponse.
private static ErrorResponse createErrorResponse(HttpStatus status, String message, ErrorResponseBuilder errorResponseBuilder) {
ErrorResponse errorResponse = new ErrorResponse();
ErrorResponseFormat errorResponseFormat = errorResponseBuilder.getErrorResponseFormat();
if (errorResponseFormat.showStatusCodeInBody()) {
errorResponse.setStatus(status.getCode());
}
if (errorResponseFormat.showMessage()) {
errorResponse.setMessage(message);
}
return errorResponse;
}
Aggregations