use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.
the class TestExceptionsResource method testException.
@SuppressWarnings("deprecation")
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "exceptionHandlingModesDataProvider")
public void testException(boolean explicit, ErrorHandlingBehavior errorHandlingBehavior, RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
Response<Greeting> response = null;
RestLiResponseException exception = null;
try {
Request<Greeting> readRequest = builders.get().id(1L).build();
ResponseFuture<Greeting> future;
if (explicit) {
future = getClient().sendRequest(readRequest, errorHandlingBehavior);
} else {
future = getClient().sendRequest(readRequest);
}
response = future.getResponse();
if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
Assert.fail("expected exception");
}
} catch (RestLiResponseException e) {
if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
exception = e;
} else {
Assert.fail("not expected exception");
}
}
if (explicit && errorHandlingBehavior == ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS) {
Assert.assertNotNull(response);
Assert.assertTrue(response.hasError());
exception = response.getError();
Assert.assertEquals(response.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
Assert.assertNull(response.getEntity());
}
Assert.assertNotNull(exception);
Assert.assertFalse(exception.hasDecodedResponse());
Assert.assertEquals(exception.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
Assert.assertEquals(exception.getServiceErrorCode(), 42);
Assert.assertEquals(exception.getServiceErrorMessage(), "error processing request");
Assert.assertTrue(exception.getServiceErrorStackTrace().contains("at com.linkedin.restli.examples.greetings.server.ExceptionsResource.get("));
Assert.assertEquals(exception.getCode(), "PROCESSING_ERROR");
Assert.assertEquals(exception.getDocUrl(), "https://example.com/errors/processing-error");
Assert.assertEquals(exception.getRequestId(), "xyz123");
Assert.assertEquals(exception.getErrorDetailType(), Greeting.class.getCanonicalName());
}
use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.
the class TestGreetingsClient method getBatchTestDataSerially.
/**
* Fetches Greetings from the server serially.
*
* @param idsToGet the ids for the Greetings to fetch
*
* @return the fetched Greetings
* @throws RemoteInvocationException
*/
private List<Greeting> getBatchTestDataSerially(RootBuilderWrapper<Long, Greeting> builders, List<Long> idsToGet) throws RemoteInvocationException {
List<Greeting> fetchedGreetings = new ArrayList<>();
for (int i = 0; i < idsToGet.size(); i++) {
try {
Long id = idsToGet.get(i);
Request<Greeting> request = builders.get().id(id).build();
ResponseFuture<Greeting> future = getClient().sendRequest(request);
Response<Greeting> greetingResponse = future.getResponse();
Greeting fetchedGreeting = greetingResponse.getEntity();
fetchedGreetings.add(fetchedGreeting);
} catch (RestLiResponseException ex) {
if (ex.getStatus() == HttpStatus.S_404_NOT_FOUND.getCode()) {
fetchedGreetings.add(null);
} else {
throw ex;
}
}
}
return fetchedGreetings;
}
use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.
the class TestFilters method testGetOldBuilders.
/**
* This is a simple test that verifies the behavior of request and response filters. This test
* hooks up two filters, one request filter and one response filter to the greeting resource.
*
* The behavior of the request filter is such that if the incoming request is of type create, the
* filter modifies the incoming create request as follows:
*
* 1. If the tone of the incoming greeting is friendly, the filter modifies it to sincere.
*
* 2. If the tone of the incoming greeting is sincere, the filter modifies it to insulting.
*
* 3. If the tone of the incoming greeting is insulting, the filter throws an exception saying
* that creation of a greeting with an insulting tone is not permitted. The HTTP status code is
* set to 403.
*
* The behavior of the response filter is as follows:
*
* 1. If the response is an error, and the HTTP status code is 403, the filter updates the
* outgoing error message and sets the status code to 400.
*
* 2. If the response is not an error, and the incoming request is a get, then the response filter
* modifies the tone of the outgoing greeting message as follows:
*
* a. If the tone of the outgoing greeting from the resource is sincere, the filter modifies it to
* friendly.
*
* b. If the tone of the outgoing greeting from the resource is insulting, the filter modifies it
* to sincere.
*
* @param builders type of request builder.
* @param tone tone of the greeting to be created.
* @param responseFilter flag indicating whether or not the response filter is to be hooked up. NOTE: The
* request filter is always hooked up.
* @param responseFilterException the exception the response filter will throw.
* @throws Exception if anything unexpected happens.
*/
@Test(dataProvider = "requestBuilderDataProvider")
public void testGetOldBuilders(RootBuilderWrapper<Long, Greeting> builders, Tone tone, boolean responseFilter, RuntimeException responseFilterException) throws Exception {
setupFilters(responseFilter, responseFilterException);
Greeting greeting = generateTestGreeting("Test greeting.....", tone);
Long createdId = null;
try {
createdId = createTestData(builders, greeting);
} catch (RestLiResponseException e) {
if (tone != Tone.INSULTING) {
fail();
}
if (responseFilter) {
assertEquals(e.getServiceErrorMessage(), RESP_FILTER_ERROR_MESSAGE);
assertEquals(e.getResponse().getStatus(), RESP_FILTER_ERROR_STATUS.getCode());
} else {
assertEquals(e.getServiceErrorMessage(), REQ_FILTER_ERROR_MESSAGE);
assertEquals(e.getResponse().getStatus(), REQ_FILTER_ERROR_STATUS.getCode());
}
verifyFilters(tone, responseFilter);
return;
}
if (tone == Tone.INSULTING) {
fail();
}
if (!responseFilter) {
greeting.setTone(mapToneForIncomingRequest(tone));
}
greeting.setId(createdId);
Request<Greeting> getRequest = builders.get().id(createdId).build();
Greeting getReturnedGreeting = getClient().sendRequest(getRequest).getResponse().getEntity();
ValidateDataAgainstSchema.validate(getReturnedGreeting.data(), getReturnedGreeting.schema(), new ValidationOptions());
assertEquals(getReturnedGreeting, greeting);
deleteAndVerifyTestData(builders, createdId);
verifyFilters(tone, responseFilter);
}
use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.
the class TestExceptionsResource2 method testGet.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "exceptionHandlingModesDataProvider")
public void testGet(boolean explicit, ErrorHandlingBehavior errorHandlingBehavior, RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
Response<Greeting> response = null;
RestLiResponseException exception = null;
try {
final Request<Greeting> req = builders.get().id(1L).build();
ResponseFuture<Greeting> future;
if (explicit) {
future = getClient().sendRequest(req, errorHandlingBehavior);
} else {
future = getClient().sendRequest(req);
}
response = future.getResponse();
if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
Assert.fail("expected exception");
}
} catch (RestLiResponseException e) {
if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
exception = e;
} else {
Assert.fail("not expected exception");
}
}
if (explicit && errorHandlingBehavior == ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS) {
Assert.assertNotNull(response);
Assert.assertTrue(response.hasError());
exception = response.getError();
Assert.assertEquals(response.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
Assert.assertNotNull(response.getEntity());
Assert.assertEquals(response.getEntity(), new Greeting().setMessage("Hello, sorry for the mess"));
}
Assert.assertNotNull(exception);
Assert.assertTrue(exception.hasDecodedResponse());
Assert.assertEquals(exception.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
final DataMap respEntityMap = DataMapUtils.readMap(exception.getResponse());
Assert.assertEquals(respEntityMap, new Greeting().setMessage("Hello, sorry for the mess").data());
}
use of com.linkedin.restli.client.RestLiResponseException in project rest.li by linkedin.
the class TestExceptionsResource2 method testExceptionWithoutValue.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "exceptionHandlingModesDataProvider")
public void testExceptionWithoutValue(boolean explicit, ErrorHandlingBehavior errorHandlingBehavior, RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
Response<Void> response = null;
RestLiResponseException exception = null;
final Request<Void> req = builders.<Void>action("ExceptionWithoutValue").build();
try {
ResponseFuture<Void> future;
if (explicit) {
future = getClient().sendRequest(req, errorHandlingBehavior);
} else {
future = getClient().sendRequest(req);
}
response = future.getResponse();
if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
Assert.fail("expected exception");
}
} catch (RestLiResponseException e) {
if (!explicit || errorHandlingBehavior == ErrorHandlingBehavior.FAIL_ON_ERROR) {
exception = e;
} else {
Assert.fail("not expected exception");
}
}
if (explicit && errorHandlingBehavior == ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS) {
Assert.assertNotNull(response);
Assert.assertTrue(response.hasError());
exception = response.getError();
Assert.assertEquals(response.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
Assert.assertNull(response.getEntity());
}
Assert.assertNotNull(exception);
Assert.assertTrue(exception.hasDecodedResponse());
Assert.assertEquals(exception.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
}
Aggregations