Search in sources :

Example 31 with RestLiResponseException

use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.

the class TestMockFailedResponseFutureBuilder method testBuildWithErrorResponseTreatServerErrorAsSuccess.

@Test
public void testBuildWithErrorResponseTreatServerErrorAsSuccess() {
    ResponseFuture<Greeting> future = buildWithErrorResponse(ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS);
    try {
        Response<Greeting> response = future.getResponse();
        Assert.assertNull(response.getEntity());
        Assert.assertEquals(response.getStatus(), 404);
        RestLiResponseException restLiResponseException = response.getError();
        Assert.assertEquals(restLiResponseException.getStatus(), 404);
        Assert.assertEquals(restLiResponseException.getServiceErrorMessage(), "foo");
    } catch (Exception e) {
        Assert.fail("No exception should have been thrown!");
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) Test(org.testng.annotations.Test)

Example 32 with RestLiResponseException

use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.

the class TestMockRestliResponseExceptionBuilder method testBuildDefaults.

@Test
public void testBuildDefaults() {
    RestLiResponseException exception = new MockRestliResponseExceptionBuilder().build();
    RestResponse errorResponse = exception.getResponse();
    assertEquals(exception.getStatus(), 500);
    assertEquals(errorResponse.getHeader(RestConstants.HEADER_RESTLI_ERROR_RESPONSE), "true");
    assertEquals(errorResponse.getHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION), AllProtocolVersions.LATEST_PROTOCOL_VERSION.toString());
    assertTrue(errorResponse.getCookies().isEmpty());
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) MockRestliResponseExceptionBuilder(com.linkedin.restli.client.testutils.MockRestliResponseExceptionBuilder) Test(org.testng.annotations.Test)

Example 33 with RestLiResponseException

use of com.linkedin.restli.client.RestLiResponseException 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 34 with RestLiResponseException

use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.

the class ExceptionUtil method wrapThrowable.

static RemoteInvocationException wrapThrowable(Throwable e) {
    if (e instanceof RestLiResponseException) {
        final RestLiResponseException restliException = (RestLiResponseException) e;
        final ErrorResponse errorResponse;
        try {
            errorResponse = getErrorResponse(restliException.getResponse());
        } catch (RestLiDecodingException decodingException) {
            return new RemoteInvocationException(decodingException);
        }
        return new RestLiResponseException(restliException.getResponse(), restliException.getDecodedResponse(), errorResponse, restliException);
    }
    return new RemoteInvocationException(e);
}
Also used : RestLiDecodingException(com.linkedin.restli.client.RestLiDecodingException) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 35 with RestLiResponseException

use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.

the class TestParseqBasedFluentClientApi method testDeleteFailure.

@Test
public void testDeleteFailure() throws Exception {
    Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    CompletionStage<Void> result = greetings.delete(-1L);
    CompletableFuture<Void> future = result.toCompletableFuture();
    try {
        future.get(5000, TimeUnit.MILLISECONDS);
        Assert.fail("Expected failure");
    } catch (ExecutionException e) {
        Assert.assertEquals(((RestLiResponseException) e.getCause()).getStatus(), 404);
    }
}
Also used : Greetings(com.linkedin.restli.examples.greetings.client.Greetings) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Aggregations

RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)77 Test (org.testng.annotations.Test)67 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)30 EmptyRecord (com.linkedin.restli.common.EmptyRecord)10 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)10 FlowId (org.apache.gobblin.service.FlowId)10 ExecutionException (java.util.concurrent.ExecutionException)9 StringMap (com.linkedin.data.template.StringMap)8 RestResponse (com.linkedin.r2.message.rest.RestResponse)7 RootBuilderWrapper (com.linkedin.restli.test.util.RootBuilderWrapper)6 FlowConfig (org.apache.gobblin.service.FlowConfig)6 ErrorResponse (com.linkedin.restli.common.ErrorResponse)5 HashMap (java.util.HashMap)5 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)4 MockRestliResponseExceptionBuilder (com.linkedin.restli.client.testutils.MockRestliResponseExceptionBuilder)4 CollectionResponse (com.linkedin.restli.common.CollectionResponse)4 AutoValidationWithProjectionBuilders (com.linkedin.restli.examples.greetings.client.AutoValidationWithProjectionBuilders)4 CreateGreeting (com.linkedin.restli.examples.greetings.client.CreateGreeting)4 PartialUpdateGreeting (com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting)4 GroupMembershipParam (com.linkedin.restli.examples.groups.api.GroupMembershipParam)4