use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestCapRepFilter method testRestException.
@Test
public void testRestException() throws IOException {
Path dirPath = Files.createTempDirectory("caprep-test");
CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
FilterChain fc = FilterChains.createRestChain(lastCallFilter, _filter);
RestRequest myRequest = new RestRequestBuilder(URI.create("/req1")).setEntity("123".getBytes()).build();
RestResponse myErrorResponse = new RestResponseBuilder().setStatus(400).setEntity("321".getBytes()).build();
RestException myRestException = new RestException(myErrorResponse);
_filter.capture(dirPath.toString());
RequestContext requestContext = new RequestContext();
FilterUtil.fireRestRequest(fc, myRequest, requestContext, FilterUtil.emptyWireAttrs());
FilterUtil.fireRestError(fc, myRestException, requestContext, FilterUtil.emptyWireAttrs());
lastCallFilter.reset();
_filter.replay(dirPath.toString());
FilterUtil.fireSimpleRestRequest(fc);
Assert.assertNull(lastCallFilter.getLastErr());
FilterUtil.fireRestRequest(fc, myRequest);
Assert.assertTrue(lastCallFilter.getLastErr() instanceof RestException);
Assert.assertEquals(((RestException) lastCallFilter.getLastErr()).getResponse(), myErrorResponse);
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class ReplayFilter method replayResponse.
private boolean replayResponse(RestRequest req, RequestContext requestContext, NextFilter<RestRequest, RestResponse> nextFilter) {
final RestResponse res = _db.replay(req);
if (res != null) {
_log.debug("Using cached response for request: " + req.getURI());
// We create an empty map instead of Collections.emptyMap, because upstream filters may
// try to modify the map.
final Map<String, String> wireAttrs = new HashMap<String, String>();
// exception.
if (!RestStatus.isOK(res.getStatus())) {
nextFilter.onError(new RestException(res), requestContext, wireAttrs);
} else {
nextFilter.onResponse(res, requestContext, wireAttrs);
}
return true;
}
return false;
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class LoadBalancerEchoServer method getExceptionTypeFromRequest.
public RestResponse getExceptionTypeFromRequest(String request) {
if (request.contains("PORT:" + _port)) {
Pattern pattern = Pattern.compile("EXCEPTION=(\\w+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(request);
int status = -9999;
while (matcher.find()) {
if (matcher.group(1).contains("NOT_FOUND")) {
status = RestStatus.NOT_FOUND;
} else if (matcher.group(1).contains("INTERNAL_SERVER_ERROR")) {
status = RestStatus.INTERNAL_SERVER_ERROR;
} else if (matcher.group(1).contains("BAD_REQUEST")) {
status = RestStatus.BAD_REQUEST;
}
final RestResponse res = new RestResponseBuilder().setStatus(status).build();
final RestException restException = new RestException(res);
return restException.getResponse();
}
}
return null;
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestStreamFilterAdapters method testResponseFilterAdapterChangeError.
@Test
public void testResponseFilterAdapterChangeError() {
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
}
@Override
public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
if (ex instanceof RestException) {
RestResponse res = ((RestException) ex).getResponse();
String newEntityStr = res.getEntity().asString("UTF8").replace('1', '0');
nextFilter.onError(new RestException((res.builder().setEntity(newEntityStr.getBytes()).build())), requestContext, wireAttrs);
} else {
nextFilter.onError(new IllegalStateException(), requestContext, wireAttrs);
}
}
});
fc.onStreamError(simpleStreamException("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
Throwable capturedEx = _beforeFilter.getThrowable();
Assert.assertTrue(capturedEx instanceof StreamException);
((StreamException) capturedEx).getResponse().getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail("should not happen");
}
@Override
public void onSuccess(ByteString result) {
Assert.assertEquals(result.asString("UTF8"), "02345");
}
}));
fc.onStreamError(new IllegalArgumentException(), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
capturedEx = _beforeFilter.getThrowable();
Assert.assertTrue(capturedEx instanceof IllegalStateException);
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestMessages method testToRestTransportCallbackRestException.
@Test
public void testToRestTransportCallbackRestException() {
TransportCallback<StreamResponse> streamCallback = response -> {
Assert.assertTrue(response.hasError());
Assert.assertNotNull(response.getError());
Assert.assertTrue(response.getError() instanceof StreamException);
Assert.assertNotNull(response.getWireAttributes());
Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
};
TransportCallback<RestResponse> restCallback = Messages.toRestTransportCallback(streamCallback);
RestResponseBuilder builder = new RestResponseBuilder();
builder.setEntity(DATA);
RestResponse restResponse = builder.build();
restCallback.onResponse(TransportResponseImpl.error(new RestException(restResponse, new IllegalStateException()), WIRE_ATTR));
}
Aggregations