use of net.morimekta.providence.PApplicationException in project providence by morimekta.
the class ProvidenceTest method testSerializable.
@Test
public void testSerializable() throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
PApplicationException originalCause = new PApplicationException("test", PApplicationExceptionType.BAD_SEQUENCE_ID);
ExceptionFields original = generator.generate(ExceptionFields.kDescriptor);
original.initCause(originalCause);
oos.writeObject(original);
oos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
ExceptionFields actual = (ExceptionFields) in.readObject();
assertThat(actual, is(equalToMessage(original)));
assertThat(actual.getCause(), is(originalCause));
assertThat(actual.getStackTrace(), is(original.getStackTrace()));
}
use of net.morimekta.providence.PApplicationException in project providence by morimekta.
the class HttpClientHandler method handleCall.
@Override
public <Request extends PMessage<Request, RequestField>, Response extends PMessage<Response, ResponseField>, RequestField extends PField, ResponseField extends PField> PServiceCall<Response, ResponseField> handleCall(PServiceCall<Request, RequestField> call, PService service) throws IOException {
if (call.getType() == PServiceCallType.EXCEPTION || call.getType() == PServiceCallType.REPLY) {
throw new PApplicationException("Request with invalid call type: " + call.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE);
}
long startTime = System.nanoTime();
PServiceCall<Response, ResponseField> reply = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
requestSerializer.serialize(baos, call);
ByteArrayContent content = new ByteArrayContent(requestSerializer.mediaType(), baos.toByteArray());
@Nonnull GenericUrl url = urlSupplier.get();
try {
HttpRequest request = factory.buildPostRequest(url, content);
request.getHeaders().setAccept(requestSerializer.mediaType());
HttpResponse response = request.execute();
try {
if (call.getType() == PServiceCallType.CALL) {
Serializer responseSerializer = requestSerializer;
if (response.getContentType() != null) {
try {
MediaType mediaType = MediaType.parse(response.getContentType());
responseSerializer = serializerProvider.getSerializer(mediaType.withoutParameters().toString());
} catch (IllegalArgumentException e) {
throw new PApplicationException("Unknown content-type in response: " + response.getContentType(), PApplicationExceptionType.INVALID_PROTOCOL).initCause(e);
}
}
// non 200 responses should have triggered a HttpResponseException,
// so this is safe.
reply = responseSerializer.deserialize(response.getContent(), service);
if (reply.getType() == PServiceCallType.CALL || reply.getType() == PServiceCallType.ONEWAY) {
throw new PApplicationException("Reply with invalid call type: " + reply.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE);
}
if (reply.getSequence() != call.getSequence()) {
throw new PApplicationException("Reply sequence out of order: call = " + call.getSequence() + ", reply = " + reply.getSequence(), PApplicationExceptionType.BAD_SEQUENCE_ID);
}
}
long endTime = System.nanoTime();
double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
try {
instrumentation.onComplete(duration, call, reply);
} catch (Exception ignore) {
}
return reply;
} finally {
// Ignore whatever is left of the response when returning, in
// case we did'nt read the whole response.
response.ignore();
}
} catch (HttpHostConnectException e) {
throw e;
} catch (ConnectException e) {
// The native exception is not helpful (for when using NetHttpTransport).
throw new HttpHostConnectException(e, new HttpHost(url.getHost(), url.getPort(), url.getScheme()));
}
} catch (Exception e) {
long endTime = System.nanoTime();
double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
try {
instrumentation.onTransportException(e, duration, call, reply);
} catch (Throwable ie) {
e.addSuppressed(ie);
}
throw e;
}
}
use of net.morimekta.providence.PApplicationException in project providence by morimekta.
the class HttpClientHandlerTest method testBadResponse.
@Test
@SuppressWarnings("unchecked")
public void testBadResponse() throws IOException, Failure {
TestService.Iface client = new TestService.Client(new HttpClientHandler(this::html));
try {
client.test(new Request("request"));
fail("no exception");
} catch (PApplicationException e) {
assertThat(e.getMessage(), is("Unknown content-type in response: text/html;charset=utf-8"));
assertThat(e.getId(), is(PApplicationExceptionType.INVALID_PROTOCOL));
}
client = new TestService.Client(new HttpClientHandler(this::response));
reply.set(new PServiceCall("foo", PServiceCallType.REPLY, 1, new TestServiceBypass().testResponse("foo")));
try {
client.test(new Request("request"));
fail("no exception");
} catch (SerializerException e) {
assertThat(e.getMessage(), is("No such method foo on client.TestService"));
assertThat(e.getExceptionType(), is(PApplicationExceptionType.UNKNOWN_METHOD));
assertThat(e.getMethodName(), is("foo"));
assertThat(e.getSequenceNo(), is(1));
}
reply.set(new PServiceCall("test", PServiceCallType.CALL, 2, new TestServiceBypass().testResponse("bar")));
try {
client.test(new Request("request"));
fail("no exception");
} catch (PApplicationException e) {
assertThat(e.getMessage(), is("Reply with invalid call type: CALL"));
assertThat(e.getId(), is(PApplicationExceptionType.INVALID_MESSAGE_TYPE));
}
reply.set(new PServiceCall("test", PServiceCallType.REPLY, 100, new TestServiceBypass().testResponse("baz")));
try {
client.test(new Request("request"));
fail("no exception");
} catch (PApplicationException e) {
assertThat(e.getMessage(), is("Reply sequence out of order: call = 2, reply = 100"));
assertThat(e.getId(), is(PApplicationExceptionType.BAD_SEQUENCE_ID));
}
reply.set(new PServiceCall("test", PServiceCallType.REPLY, 4, new PApplicationException("foo", PApplicationExceptionType.INTERNAL_ERROR)));
try {
client.test(new Request("request"));
fail("no exception");
} catch (SerializerException e) {
assertThat(e.getMessage(), is("Wrong type string(11) for client.TestService.test.response.fail, should be struct(12)"));
assertThat(e.getExceptionType(), is(PApplicationExceptionType.PROTOCOL_ERROR));
assertThat(e.getMethodName(), is("test"));
assertThat(e.getSequenceNo(), is(4));
}
}
use of net.morimekta.providence.PApplicationException in project providence by morimekta.
the class DefaultProcessorHandler method process.
@Override
@SuppressWarnings("unchecked")
public void process(MessageReader reader, MessageWriter writer) throws IOException {
PServiceCall call, reply;
try {
call = reader.read(processor.getDescriptor());
} catch (SerializerException e) {
if (e.getMethodName() != null) {
LOGGER.error("Error when reading service call " + processor.getDescriptor().getName() + "." + e.getMethodName() + "()", e);
} else {
LOGGER.error("Error when reading service call " + processor.getDescriptor().getName(), e);
}
try {
PApplicationException oe = new PApplicationException(e.getMessage(), e.getExceptionType());
reply = new PServiceCall<>(e.getMethodName(), PServiceCallType.EXCEPTION, e.getSequenceNo(), oe);
writer.write(reply);
return;
} catch (Exception e2) {
e.addSuppressed(e2);
throw e;
}
}
if (call.getType() == PServiceCallType.REPLY || call.getType() == PServiceCallType.EXCEPTION) {
try {
PApplicationException oe = new PApplicationException("Invalid service request call type: " + call.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE);
reply = new PServiceCall(call.getMethod(), PServiceCallType.EXCEPTION, call.getSequence(), oe);
writer.write(reply);
return;
} catch (Exception e) {
throw new IOException("Unable to write error response", e);
}
}
try {
reply = processor.handleCall(call);
} catch (Exception e) {
LOGGER.error("Error when handling service call " + processor.getDescriptor().getName() + "." + call.getMethod() + "()", e);
try {
PApplicationException oe = new PApplicationException(e.getMessage(), PApplicationExceptionType.INTERNAL_ERROR);
reply = new PServiceCall<>(call.getMethod(), PServiceCallType.EXCEPTION, call.getSequence(), oe);
writer.write(reply);
return;
} catch (Exception e2) {
e.addSuppressed(e2);
throw e;
}
}
if (reply != null) {
try {
writer.write(reply);
} catch (SerializerException e) {
LOGGER.error("Error when replying to service call " + processor.getDescriptor().getName() + "." + call.getMethod() + "()", e);
try {
PApplicationException oe = new PApplicationException(e.getMessage(), e.getExceptionType());
reply = new PServiceCall<>(call.getMethod(), PServiceCallType.EXCEPTION, call.getSequence(), oe);
writer.write(reply);
} catch (Exception e2) {
e.addSuppressed(e2);
throw e;
}
}
}
}
use of net.morimekta.providence.PApplicationException in project providence by morimekta.
the class SocketClientHandlerTest method testSimpleRequest_wrongMethod.
@Test
public void testSimpleRequest_wrongMethod() throws IOException, TException, Failure, InterruptedException {
when(impl.test(any(net.morimekta.test.thrift.thrift.service.Request.class))).thenThrow(new net.morimekta.test.thrift.thrift.service.Failure("failure"));
MyService2.Iface client = new MyService2.Client(new SocketClientHandler(serializer, address));
try {
client.testing(new Request(null));
fail("no exception");
} catch (PApplicationException e) {
assertThat(e.getId(), is(UNKNOWN_METHOD));
assertThat(e.getMessage(), is(equalTo("Invalid method name: 'testing'")));
}
Thread.sleep(10L);
verifyZeroInteractions(impl);
}
Aggregations