Search in sources :

Example 16 with RestException

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);
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestException(com.linkedin.r2.message.rest.RestException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 17 with RestException

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>());
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) ServerRetryFilter(com.linkedin.r2.filter.transport.ServerRetryFilter) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) RestException(com.linkedin.r2.message.rest.RestException) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 18 with RestException

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);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 19 with RestException

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());
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestException(com.linkedin.r2.message.rest.RestException) RestResponse(com.linkedin.r2.message.rest.RestResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) TimeUnit(java.util.concurrent.TimeUnit) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 20 with RestException

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;
}
Also used : RestException(com.linkedin.r2.message.rest.RestException) TimeUnit(java.util.concurrent.TimeUnit) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

RestException (com.linkedin.r2.message.rest.RestException)62 RestResponse (com.linkedin.r2.message.rest.RestResponse)48 Test (org.testng.annotations.Test)44 RestRequest (com.linkedin.r2.message.rest.RestRequest)33 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)31 URI (java.net.URI)23 BeforeTest (org.testng.annotations.BeforeTest)21 ExecutionException (java.util.concurrent.ExecutionException)20 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)19 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)19 RequestContext (com.linkedin.r2.message.RequestContext)18 Callback (com.linkedin.common.callback.Callback)16 URISyntaxException (java.net.URISyntaxException)16 ByteString (com.linkedin.data.ByteString)15 AfterTest (org.testng.annotations.AfterTest)12 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)11 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)11 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)11 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)11 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)11