use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class MessageStreamsTest method testFileCollector.
@Test
public void testFileCollector() throws IOException {
File file = tmp.newFile("tmp");
Collector<CompactFields, OutputStream, Integer> collector = MessageCollectors.toFile(file, new PrettySerializer().config());
OutputStream out = mock(OutputStream.class);
doThrow(new IOException("oops")).when(out).write(MessageStreams.READABLE_ENTRY_SEP);
doThrow(new IOException("close")).when(out).close();
assertThat(collector.combiner().apply(out, out), is(sameInstance(out)));
try {
collector.accumulator().accept(out, list.get(0));
fail("no exception");
} catch (UncheckedIOException e) {
assertThat(e.getMessage(), is("Unable to write to tmp"));
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), is("oops"));
}
try {
collector.finisher().apply(out);
fail("no exception");
} catch (UncheckedIOException e) {
assertThat(e.getMessage(), is("Unable to close tmp"));
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), is("close"));
}
Serializer ms = mock(Serializer.class);
doThrow(new SerializerException("oops2")).when(ms).serialize(out, list.get(0));
collector = MessageCollectors.toFile(file, ms);
try {
collector.accumulator().accept(out, list.get(0));
fail("no exception");
} catch (UncheckedIOException e) {
assertThat(e.getMessage(), is("Bad data"));
assertThat(e.getCause(), is(instanceOf(SerializerException.class)));
assertThat(e.getCause().getMessage(), is("oops2"));
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class ProvidenceConfigParserTest method testCircularIncludes.
@Test
public void testCircularIncludes() throws IOException {
File a = temp.newFile("a.cfg");
File b = temp.newFile("b.cfg");
File c = temp.newFile("c.cfg");
writeContentTo("include \"b.cfg\" as a\n" + "config.Database {}\n", a);
writeContentTo("include \"c.cfg\" as a\n" + "config.Database {}\n", b);
writeContentTo("include \"a.cfg\" as a\n" + "config.Database {}\n", c);
ProvidenceConfigParser config = new ProvidenceConfigParser(registry, false);
try {
config.parseConfig(a.toPath(), null);
fail("no exception on circular deps");
} catch (SerializerException e) {
assertEquals("Circular includes detected: a.cfg -> b.cfg -> c.cfg -> a.cfg", e.getMessage());
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class ProvidenceConfigParserTest method testIncludeNoSuchFile.
@Test
public void testIncludeNoSuchFile() throws IOException {
File a = temp.newFile("a.cfg");
writeContentTo("include \"b.cfg\" as a\n" + "config.Database {}\n", a);
ProvidenceConfigParser config = new ProvidenceConfigParser(registry, false);
try {
config.parseConfig(a.toPath(), null);
fail("no exception on circular deps");
} catch (SerializerException e) {
assertEquals("Included file \"b.cfg\" not found.", e.getMessage());
}
}
use of net.morimekta.providence.serializer.SerializerException 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.serializer.SerializerException 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;
}
}
}
}
Aggregations