use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class RestClientTest method testRestLiRemoteInvocationException.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestOptions")
public void testRestLiRemoteInvocationException(SendRequestOption option, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws ExecutionException, TimeoutException, InterruptedException, RestLiDecodingException {
final int HTTP_CODE = 404;
final String ERR_MSG = "WHOOPS!";
RestClient client = mockClient(HTTP_CODE, ERR_MSG, protocolVersion);
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) {
Throwable cause = e.getCause();
Assert.assertTrue(cause instanceof RemoteInvocationException, "Expected RemoteInvocationException not " + cause.getClass().getName());
RemoteInvocationException rlre = (RemoteInvocationException) cause;
Assert.assertTrue(rlre.getMessage().startsWith("Received error " + HTTP_CODE + " from server"));
Throwable rlCause = rlre.getCause();
Assert.assertTrue(rlCause instanceof RestException, "Excepted RestException not " + rlCause.getClass().getName());
RestException rle = (RestException) rlCause;
Assert.assertEquals(ERR_MSG, rle.getResponse().getEntity().asString("UTF-8"));
Assert.assertEquals(HTTP_CODE, rle.getResponse().getStatus());
}
}
use of com.linkedin.r2.message.rest.RestException 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.r2.message.rest.RestException in project rest.li by linkedin.
the class TestServerTimeout method testServerThrowButShouldNotTimeout.
@Test
public void testServerThrowButShouldNotTimeout() throws Exception {
RestRequest request = new RestRequestBuilder(Bootstrap.createHttpURI(PORT, THROW_BUT_SHOULD_NOT_TIMEOUT_URI)).setEntity(new byte[10240]).build();
_client.restRequest(request);
Future<RestResponse> futureResponse = _client.restRequest(request);
// if server times out, our second request would fail with TimeoutException because it's blocked by first one
try {
futureResponse.get(SERVER_IOHANDLER_TIMEOUT / 2, TimeUnit.MILLISECONDS);
Assert.fail("Should fail with ExecutionException");
} catch (ExecutionException ex) {
Assert.assertTrue(ex.getCause() instanceof RestException);
RestException restException = (RestException) ex.getCause();
Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("Server throw for test."));
}
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestServerTimeout method testFilterThrowButShouldNotTimeout.
@Test
public void testFilterThrowButShouldNotTimeout() throws Exception {
RestRequest request = new RestRequestBuilder(Bootstrap.createHttpURI(PORT, BUGGY_FILTER_URI)).setEntity(new byte[10240]).build();
_client.restRequest(request);
Future<RestResponse> futureResponse = _client.restRequest(request);
// if server times out, our second request would fail with TimeoutException because it's blocked by first one
try {
futureResponse.get(SERVER_IOHANDLER_TIMEOUT / 2, TimeUnit.MILLISECONDS);
Assert.fail("Should fail with ExecutionException");
} catch (ExecutionException ex) {
Assert.assertTrue(ex.getCause() instanceof RestException);
RestException restException = (RestException) ex.getCause();
Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("Buggy filter throws."));
}
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestServerTimeoutAsyncEvent method testServerThrowButShouldNotTimeout.
@Test
public void testServerThrowButShouldNotTimeout() throws Exception {
RestRequest request = new RestRequestBuilder(Bootstrap.createHttpURI(PORT, THROW_BUT_SHOULD_NOT_TIMEOUT_URI)).setEntity(new byte[10240]).build();
_client.restRequest(request);
Future<RestResponse> futureResponse = _client.restRequest(request);
// if server times out, our second request would fail with TimeoutException because it's blocked by first one
try {
futureResponse.get(ASYNC_EVENT_TIMEOUT / 2, TimeUnit.MILLISECONDS);
Assert.fail("Should fail with ExecutionException");
} catch (ExecutionException ex) {
Assert.assertTrue(ex.getCause() instanceof RestException);
RestException restException = (RestException) ex.getCause();
Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("Server throw for test."));
}
}
Aggregations