Search in sources :

Example 1 with SerializerException

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"));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File) SerializerException(net.morimekta.providence.serializer.SerializerException) CompactFields(net.morimekta.test.providence.core.CompactFields) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Serializer(net.morimekta.providence.serializer.Serializer) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) Test(org.junit.Test)

Example 2 with SerializerException

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());
    }
}
Also used : File(java.io.File) SerializerException(net.morimekta.providence.serializer.SerializerException) Test(org.junit.Test)

Example 3 with SerializerException

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());
    }
}
Also used : File(java.io.File) SerializerException(net.morimekta.providence.serializer.SerializerException) Test(org.junit.Test)

Example 4 with SerializerException

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));
    }
}
Also used : TestService(net.morimekta.test.providence.client.TestService) PApplicationException(net.morimekta.providence.PApplicationException) PServiceCall(net.morimekta.providence.PServiceCall) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(net.morimekta.test.providence.client.Request) SerializerException(net.morimekta.providence.serializer.SerializerException) Test(org.junit.Test)

Example 5 with SerializerException

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;
            }
        }
    }
}
Also used : PServiceCall(net.morimekta.providence.PServiceCall) PApplicationException(net.morimekta.providence.PApplicationException) IOException(java.io.IOException) SerializerException(net.morimekta.providence.serializer.SerializerException) PApplicationException(net.morimekta.providence.PApplicationException) IOException(java.io.IOException) SerializerException(net.morimekta.providence.serializer.SerializerException)

Aggregations

SerializerException (net.morimekta.providence.serializer.SerializerException)26 TException (org.apache.thrift.TException)8 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)8 TTransport (org.apache.thrift.transport.TTransport)8 IOException (java.io.IOException)7 PServiceCall (net.morimekta.providence.PServiceCall)6 File (java.io.File)5 UncheckedIOException (java.io.UncheckedIOException)5 PMessage (net.morimekta.providence.PMessage)5 PField (net.morimekta.providence.descriptor.PField)5 TMessage (org.apache.thrift.protocol.TMessage)5 Nonnull (javax.annotation.Nonnull)4 ArgumentException (net.morimekta.console.args.ArgumentException)4 PApplicationException (net.morimekta.providence.PApplicationException)4 Test (org.junit.Test)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ArgumentParser (net.morimekta.console.args.ArgumentParser)3 CountingOutputStream (net.morimekta.util.io.CountingOutputStream)3 TProtocol (org.apache.thrift.protocol.TProtocol)3 TTupleProtocol (org.apache.thrift.protocol.TTupleProtocol)3