Search in sources :

Example 6 with PApplicationException

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()));
}
Also used : ExceptionFields(net.morimekta.test.providence.testing.ExceptionFields) ByteArrayInputStream(java.io.ByteArrayInputStream) PApplicationException(net.morimekta.providence.PApplicationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 7 with PApplicationException

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;
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) Nonnull(javax.annotation.Nonnull) HttpResponse(com.google.api.client.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericUrl(com.google.api.client.http.GenericUrl) PApplicationException(net.morimekta.providence.PApplicationException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) HttpResponse(com.google.api.client.http.HttpResponse) HttpHost(org.apache.http.HttpHost) PApplicationException(net.morimekta.providence.PApplicationException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) MediaType(com.google.common.net.MediaType) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Serializer(net.morimekta.providence.serializer.Serializer) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ConnectException(java.net.ConnectException)

Example 8 with PApplicationException

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));
    }
}
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 9 with PApplicationException

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;
            }
        }
    }
}
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)

Example 10 with PApplicationException

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);
}
Also used : MyService2(net.morimekta.test.providence.thrift.service.MyService2) PApplicationException(net.morimekta.providence.PApplicationException) Request(net.morimekta.test.providence.thrift.service.Request) Test(org.junit.Test)

Aggregations

PApplicationException (net.morimekta.providence.PApplicationException)17 PServiceCall (net.morimekta.providence.PServiceCall)11 IOException (java.io.IOException)8 Test (org.junit.Test)7 PServiceCallType (net.morimekta.providence.PServiceCallType)6 PServiceMethod (net.morimekta.providence.descriptor.PServiceMethod)6 Nonnull (javax.annotation.Nonnull)5 PMessage (net.morimekta.providence.PMessage)5 PField (net.morimekta.providence.descriptor.PField)5 SerializerException (net.morimekta.providence.serializer.SerializerException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Nullable (javax.annotation.Nullable)2 PProcessor (net.morimekta.providence.PProcessor)2 PService (net.morimekta.providence.descriptor.PService)2 TException (org.apache.thrift.TException)2 TMessage (org.apache.thrift.protocol.TMessage)2