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) {
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setStatus(status.getCode());
errorResponse.setMessage(message);
return errorResponse;
}
use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class RestClientTest method testRestLiResponseExceptionCallback.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestOptions")
public void testRestLiResponseExceptionCallback(SendRequestOption option, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws ExecutionException, TimeoutException, InterruptedException, RestLiDecodingException {
final String ERR_KEY = "someErr";
final String ERR_VALUE = "WHOOPS!";
final String ERR_MSG = "whoops2";
final int HTTP_CODE = 400;
final int APP_CODE = 666;
RestClient client = mockClient(ERR_KEY, ERR_VALUE, ERR_MSG, HTTP_CODE, APP_CODE, protocolVersion, errorResponseHeaderName);
Request<EmptyRecord> request = mockRequest(EmptyRecord.class, versionOption);
RequestBuilder<Request<EmptyRecord>> requestBuilder = mockRequestBuilder(request);
FutureCallback<Response<EmptyRecord>> callback = new FutureCallback<Response<EmptyRecord>>();
try {
sendRequest(option, client, request, requestBuilder, callback);
Long l = timeoutOption._l;
TimeUnit timeUnit = timeoutOption._timeUnit;
Response<EmptyRecord> response = l == null ? callback.get() : callback.get(l, timeUnit);
Assert.fail("Should have thrown");
} catch (ExecutionException e) {
// New
Throwable cause = e.getCause();
Assert.assertTrue(cause instanceof RestLiResponseException, "Expected RestLiResponseException not " + cause.getClass().getName());
RestLiResponseException rlre = (RestLiResponseException) cause;
Assert.assertEquals(HTTP_CODE, rlre.getStatus());
Assert.assertEquals(ERR_VALUE, rlre.getErrorDetails().get(ERR_KEY));
Assert.assertEquals(APP_CODE, rlre.getServiceErrorCode());
Assert.assertEquals(ERR_MSG, rlre.getServiceErrorMessage());
// Old
Assert.assertTrue(cause instanceof RestException, "Expected RestException not " + cause.getClass().getName());
RestException re = (RestException) cause;
RestResponse r = re.getResponse();
ErrorResponse er = new EntityResponseDecoder<ErrorResponse>(ErrorResponse.class).decodeResponse(r).getEntity();
Assert.assertEquals(HTTP_CODE, r.getStatus());
Assert.assertEquals(ERR_VALUE, er.getErrorDetails().data().getString(ERR_KEY));
Assert.assertEquals(APP_CODE, er.getServiceErrorCode().intValue());
Assert.assertEquals(ERR_MSG, er.getMessage());
}
}
use of com.linkedin.restli.common.ErrorResponse in project parseq by linkedin.
the class GetRequestGroup method unbatchResponse.
private static <K, RT extends RecordTemplate> Response<RT> unbatchResponse(BatchGetEntityRequest<K, RT> request, Response<BatchKVResponse<K, EntityResponse<RT>>> batchResponse, Object id) throws RemoteInvocationException {
final BatchKVResponse<K, EntityResponse<RT>> batchEntity = batchResponse.getEntity();
final ErrorResponse errorResponse = batchEntity.getErrors().get(id);
if (errorResponse != null) {
throw new RestLiResponseException(errorResponse);
}
final EntityResponse<RT> entityResponse = batchEntity.getResults().get(id);
if (entityResponse != null) {
final RT entityResult = entityResponse.getEntity();
if (entityResult != null) {
return new ResponseImpl<>(batchResponse, entityResult);
}
}
LOGGER.debug("No result or error for base URI : {}, id: {}. Verify that the batchGet endpoint returns response keys that match batchGet request IDs.", request.getBaseUriTemplate(), id);
throw NOT_FOUND_EXCEPTION;
}
use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class TestBatchKVResponse method testDeserialization.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchKVResponseDataProvider")
public <K> void testDeserialization(List<K> keys, Class<K> keyClass, Class<? extends RecordTemplate> keyKeyClass, Class<? extends RecordTemplate> keyParamsClass, ProtocolVersion protocolVersion) {
final K resultKey = keys.get(0);
final K errorKey = keys.get(1);
final String resultKeyString = URIParamUtils.encodeKeyForBody(resultKey, false, protocolVersion);
final String errorKeyString = URIParamUtils.encodeKeyForBody(errorKey, false, protocolVersion);
final DataMap inputResults = new DataMap();
inputResults.put(resultKeyString, _record.data());
final DataMap inputErrors = new DataMap();
inputErrors.put(errorKeyString, _error.data());
final DataMap testData = new DataMap();
testData.put(BatchKVResponse.RESULTS, inputResults);
testData.put(BatchKVResponse.ERRORS, inputErrors);
final BatchKVResponse<K, TestRecord> response = new BatchKVResponse<>(testData, keyClass, TestRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap(), keyKeyClass, keyParamsClass, protocolVersion);
final Map<K, TestRecord> outputResults = response.getResults();
final TestRecord outRecord = outputResults.get(resultKey);
Assert.assertEquals(outRecord, outRecord);
final Map<K, ErrorResponse> outputErrors = response.getErrors();
final ErrorResponse outError = outputErrors.get(errorKey);
Assert.assertEquals(outError, _error);
}
use of com.linkedin.restli.common.ErrorResponse in project rest.li by linkedin.
the class TestBatchEntityResponseDecoder method testDecoding.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchEntityResponseDataProvider")
public void testDecoding(List<String> keys, ProtocolVersion protocolVersion) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
final String resultKey = keys.get(0);
final String statusKey = keys.get(1);
final String errorKey = keys.get(2);
final DataMap resultData = new DataMap();
resultData.put(resultKey, _record.data());
final DataMap statusData = new DataMap();
statusData.put(statusKey, _status.getCode());
final DataMap errorData = new DataMap();
errorData.put(errorKey, _error.data());
final DataMap data = new DataMap();
data.put(BatchResponse.RESULTS, resultData);
data.put(BatchResponse.STATUSES, statusData);
data.put(BatchResponse.ERRORS, errorData);
final BatchEntityResponseDecoder<String, TestRecord> decoder = new BatchEntityResponseDecoder<>(new TypeSpec<>(TestRecord.class), new TypeSpec<>(String.class), Collections.<String, CompoundKey.TypeInfo>emptyMap(), null);
final BatchKVResponse<String, EntityResponse<TestRecord>> response = decoder.wrapResponse(data, Collections.<String, String>emptyMap(), protocolVersion);
final Map<String, EntityResponse<TestRecord>> results = response.getResults();
final Map<String, ErrorResponse> errors = response.getErrors();
final Collection<String> uniqueKeys = new HashSet<>(keys);
Assert.assertEquals(results.size(), uniqueKeys.size());
Assert.assertEquals(errors.size(), 1);
Assert.assertEquals(results.get(resultKey).getEntity(), _record);
Assert.assertEquals(results.get(statusKey).getStatus(), _status);
Assert.assertEquals(results.get(errorKey).getError(), _error);
Assert.assertEquals(errors.get(errorKey), _error);
// Check that the response still contains the original data map
Assert.assertEquals(response.data(), data);
}
Aggregations