use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class AbstractEchoServiceTest method testBadRestURI.
@Test
public void testBadRestURI() {
final EchoService client = getEchoClient(_client, URI.create("/unknown-service"));
if (!(client instanceof RestEchoClient)) {
return;
}
final String msg = "This is a simple echo message";
final FutureCallback<String> callback = new FutureCallback<String>();
client.echo(msg, callback);
try {
callback.get();
Assert.fail("Should have thrown an exception");
} catch (Exception e) {
Assert.assertTrue(e instanceof ExecutionException);
Assert.assertTrue(e.getCause() instanceof RestException);
RestException re = (RestException) e.getCause();
Assert.assertEquals(re.getResponse().getStatus(), RestStatus.NOT_FOUND);
}
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class MultiplexedCallback method notifyIndividualCallbacks.
private void notifyIndividualCallbacks(Response<MultiplexedResponseContent> muxResponse) {
IndividualResponseMap individualResponses = muxResponse.getEntity().getResponses();
for (IndividualResponseMap.Entry<String, IndividualResponse> individualResponseMapEntry : individualResponses.entrySet()) {
Integer id = Integer.valueOf(individualResponseMapEntry.getKey());
IndividualResponse individualResponse = individualResponseMapEntry.getValue();
Callback<RestResponse> callback = _callbacks.get(id);
RestResponse individualRestResponse;
try {
individualRestResponse = buildIndividualRestResponse(muxResponse, individualResponse);
} catch (MimeTypeParseException e) {
callback.onError(new RestLiDecodingException("Could not convert IndividualResponse to individual RestRestponse due to an invalid content type, id=" + id, e));
return;
} catch (IOException e) {
callback.onError(new RestLiDecodingException("Could not convert IndividualResponse to individual RestRestponse, id=" + id, e));
return;
}
if (RestStatus.isOK(individualResponse.getStatus())) {
callback.onSuccess(individualRestResponse);
} else {
RestException exception = new RestException(individualRestResponse, "Received error " + individualRestResponse.getStatus());
callback.onError(exception);
}
}
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestMultiplexedCallback method testMixed.
@Test
public void testMixed() throws Exception {
FutureCallback<RestResponse> callback1 = new FutureCallback<RestResponse>();
FutureCallback<RestResponse> callback2 = new FutureCallback<RestResponse>();
ImmutableMap<Integer, Callback<RestResponse>> individualCallbacks = ImmutableMap.<Integer, Callback<RestResponse>>of(ID1, callback1, ID2, callback2);
FutureCallback<MultiplexedResponse> aggregatedCallback = new FutureCallback<MultiplexedResponse>();
TestRecord entity1 = fakeEntity(ID1);
IndividualResponse ir1 = fakeIndividualResponse(entity1);
IndividualResponse ir2 = fakeIndividualErrorResponse();
MultiplexedResponseContent responseContent = new MultiplexedResponseContent().setResponses(new IndividualResponseMap(ImmutableMap.of(Integer.toString(ID1), ir1, Integer.toString(ID2), ir2)));
MultiplexedCallback multiplexedCallback = new MultiplexedCallback(individualCallbacks, aggregatedCallback);
multiplexedCallback.onSuccess(fakeRestResponse(responseContent));
assertRestResponseEquals(callback1.get(), fakeRestResponse(entity1));
RestException actualError = (RestException) getError(callback2);
assertRestResponseEquals(actualError.getResponse(), fakeRestErrorResponse());
MultiplexedResponse multiplexedResponse = aggregatedCallback.get();
Assert.assertEquals(multiplexedResponse.getStatus(), HttpStatus.S_200_OK.getCode());
Assert.assertEquals(multiplexedResponse.getHeaders(), HEADERS);
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestGreetingsClientProtocolVersionHeader method testNoProtocolVersionHeaderFail.
@Test
public void testNoProtocolVersionHeaderFail() throws InterruptedException {
final TransportClientAdapter client = new TransportClientAdapter(CLIENT_FACTORY.getClient(Collections.<String, String>emptyMap()));
final RestRequestBuilder requestBuilder = new RestRequestBuilder(URI.create(URI_PREFIX));
final RestRequest request = requestBuilder.build();
Assert.assertTrue(request.getHeaders().isEmpty());
try {
client.restRequest(request).get();
} catch (ExecutionException e) {
final RestException exception = (RestException) e.getCause();
final RestResponse response = exception.getResponse();
Assert.assertEquals(response.getStatus(), HttpStatus.S_404_NOT_FOUND.getCode());
Assert.assertEquals(response.getHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION), AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion().toString());
final DataMap exceptionDetail = DataMapUtils.readMap(response.getEntity().asInputStream());
Assert.assertEquals(exceptionDetail.getString("exceptionClass"), RestLiServiceException.class.getName());
}
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class RestLiServer method respondWithPreRoutingError.
private void respondWithPreRoutingError(Throwable th, RestRequest request, RestLiAttachmentReader attachmentReader, RequestExecutionCallback<RestResponse> callback) {
RestLiFilterResponseContextFactory<Object> filterResponseContextFactory = new RestLiFilterResponseContextFactory<Object>(request, null, _responseHandler);
RestLiResponseData responseData = filterResponseContextFactory.fromThrowable(th).getResponseData();
RestException restException = _responseHandler.buildRestException(th, _responseHandler.buildPartialResponse(null, responseData));
callback.onError(restException, createEmptyExecutionReport(), attachmentReader, null);
}
Aggregations