use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestHttpBridge method testHttpToRestErrorMessage.
@Test
public void testHttpToRestErrorMessage() throws TimeoutException, InterruptedException, ExecutionException {
FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(futureCallback);
TransportCallback<RestResponse> bridgeCallback = HttpBridge.httpToRestCallback(callback);
RestResponse restResponse = new RestResponseBuilder().build();
// Note: FutureCallback will fail if called twice. An exception would be raised on the current
// thread because we begin the callback sequence here in onResponse.
// (test originally added due to bug with double callback invocation)
bridgeCallback.onResponse(TransportResponseImpl.<RestResponse>error(new RestException(restResponse)));
RestResponse resp = futureCallback.get(30, TimeUnit.SECONDS);
// should have unpacked restResponse from the RestException that we passed in without
// propagating the actual exception
Assert.assertSame(resp, restResponse);
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestServerRetryFilter method testRetryFilter.
@Test
public void testRetryFilter() {
String retryMessage = "this is a retry";
ServerRetryFilter retryFilter = new ServerRetryFilter();
RestFilter captureFilter = new RestFilter() {
@Override
public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
Assert.assertEquals(wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY), retryMessage);
}
};
FilterChain filterChain = FilterChains.createRestChain(captureFilter, retryFilter);
FilterUtil.fireRestError(filterChain, new RestException(null, new RetriableRequestException(retryMessage)), new HashMap<String, String>());
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class ExceptionUtil method exceptionForThrowable.
public static RemoteInvocationException exceptionForThrowable(Throwable e, RestResponseDecoder<?> responseDecoder) {
if (e instanceof RestException) {
final RestException re = (RestException) e;
final RestResponse response = re.getResponse();
final ErrorResponse errorResponse;
// decode the response body when HEADER_RESTLI_ERROR_RESPONSE header is set.
try {
errorResponse = getErrorResponse(response);
} catch (RestLiDecodingException decodingException) {
return new RemoteInvocationException(e.getMessage(), decodingException);
}
Response<?> decodedResponse = null;
final String header = HeaderUtil.getErrorResponseHeaderValue(response.getHeaders());
if (header == null) {
// This is purely to handle case #2 commented above.
try {
decodedResponse = responseDecoder.decodeResponse(response);
} catch (RestLiDecodingException decodingException) {
return new RemoteInvocationException(e.getMessage(), e);
}
}
return new RestLiResponseException(response, decodedResponse, errorResponse, e);
}
if (e instanceof RemoteInvocationException) {
return (RemoteInvocationException) e;
}
return new RemoteInvocationException(e);
}
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 getErrorResponse.
private <T extends RecordTemplate> RestLiResponseException getErrorResponse(GetResponseOption option, ResponseFuture<T> future, TimeoutOption timeoutOption) throws InterruptedException, TimeoutException, RemoteInvocationException {
Response<T> response = null;
T entity;
RestLiResponseException result = null;
Long l = timeoutOption._l;
TimeUnit timeUnit = timeoutOption._timeUnit;
switch(option) {
case GET:
try {
response = l == null ? future.get() : future.get(l, timeUnit);
Assert.fail("Should have thrown");
} catch (ExecutionException e) {
Throwable cause = e.getCause();
Assert.assertTrue(cause instanceof RestException, "Expected RestLiResponseException not " + cause.getClass().getName());
result = (RestLiResponseException) cause;
}
break;
case GET_RESPONSE:
case GET_RESPONSE_EXPLICIT_THROW:
try {
response = l == null ? future.getResponse() : future.getResponse(l, timeUnit);
Assert.fail("Should have thrown");
} catch (RestLiResponseException e) {
result = e;
}
break;
case GET_RESPONSE_EXPLICIT_NO_THROW:
response = l == null ? future.getResponse() : future.getResponse(l, timeUnit);
result = response.getError();
break;
case GET_RESPONSE_ENTITY:
case GET_RESPONSE_ENTITY_EXPLICIT_THROW:
try {
entity = l == null ? future.getResponseEntity() : future.getResponseEntity(l, timeUnit);
Assert.fail("Should have thrown");
} catch (RestLiResponseException e) {
result = e;
}
break;
case GET_RESPONSE_ENTITY_EXPLICIT_NO_THROW:
entity = l == null ? future.getResponseEntity() : future.getResponseEntity(l, timeUnit);
break;
default:
throw new IllegalStateException();
}
return result;
}
Aggregations