Search in sources :

Example 11 with RemoteException

use of com.palantir.conjure.java.api.errors.RemoteException in project dialogue by palantir.

the class MyServiceIntegrationTest method testGetGreetingAsyncRemoteException.

@Test
public void testGetGreetingAsyncRemoteException() {
    String errorBody = "{\"errorCode\":\"FAILED_PRECONDITION\",\"errorName\":\"Default:FailedPrecondition\"," + "\"errorInstanceId\":\"839ccac1-3944-479a-bcd2-3196b5fa16ee\",\"parameters\":{\"key\":\"value\"}}";
    undertowHandler = exchange -> {
        exchange.assertMethod(HttpMethod.GET);
        exchange.assertPath("/greeting");
        exchange.assertAccept().isEqualTo("text/csv");
        exchange.assertContentType().isNull();
        exchange.assertNoBody();
        exchange.exchange.setStatusCode(500);
        exchange.setContentType("application/json");
        exchange.writeStringBody(errorBody);
    };
    assertThatThrownBy(() -> myServiceDialogue.getGreetingAsync().get()).isExactlyInstanceOf(ExecutionException.class).hasCauseExactlyInstanceOf(RemoteException.class).satisfies(executionException -> {
        RemoteException remoteException = (RemoteException) executionException.getCause();
        assertThat(remoteException.getError()).isEqualTo(SerializableError.builder().errorCode("FAILED_PRECONDITION").errorName("Default:FailedPrecondition").errorInstanceId("839ccac1-3944-479a-bcd2-3196b5fa16ee").putParameters("key", "value").build());
    });
}
Also used : HttpString(io.undertow.util.HttpString) RemoteException(com.palantir.conjure.java.api.errors.RemoteException) UnknownRemoteException(com.palantir.conjure.java.api.errors.UnknownRemoteException) Test(org.junit.jupiter.api.Test)

Example 12 with RemoteException

use of com.palantir.conjure.java.api.errors.RemoteException in project dialogue by palantir.

the class SingleParamServicesTest method runTestCase.

@Test
public void runTestCase() throws Exception {
    Assume.assumeFalse(Cases.shouldIgnore(endpointName, jsonString));
    log.info("Invoking {} {}({})", SafeArg.of("serviceName", serviceName), SafeArg.of("endpointName", endpointName), SafeArg.of("jsonString", jsonString));
    Object service = servicesMaps.get(serviceName);
    for (Method method : servicesMaps.get(serviceName).getClass().getMethods()) {
        String name = method.getName();
        // Need to set accessible true work around dialogues anonymous class impl
        method.setAccessible(true);
        if (method.getParameterCount() == 1) {
            // conjure-java generates `default` methods for optional query params, we don't want to call these
            continue;
        }
        if (endpointName.get().equals(name)) {
            try {
                // HACKHACK, index parameter order is different for different services.
                if ("singleHeaderService".equals(serviceName)) {
                    Type type = getRawType(method.getGenericParameterTypes()[0]);
                    Class<?> cls = ClassUtils.getClass(type.getTypeName());
                    method.invoke(service, objectMapper.readValue(jsonString, cls), index);
                } else {
                    Type type = getRawType(method.getGenericParameterTypes()[1]);
                    Class<?> cls = ClassUtils.getClass(type.getTypeName());
                    method.invoke(service, index, objectMapper.readValue(jsonString, cls));
                }
                log.info("Successfully post param to endpoint {} and index {}", SafeArg.of("endpointName", endpointName), SafeArg.of("index", index));
            } catch (RemoteException e) {
                log.error("Caught exception with params: {}", SafeArg.of("errorParameters", e.getError().parameters()), e);
                throw e;
            }
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Method(java.lang.reflect.Method) RemoteException(com.palantir.conjure.java.api.errors.RemoteException) Test(org.junit.Test)

Example 13 with RemoteException

use of com.palantir.conjure.java.api.errors.RemoteException in project dialogue by palantir.

the class DefaultClientsBlockingTest method testRemoteException.

@Test
public void testRemoteException() {
    RemoteException remoteException = remoteException(new ServiceException(ErrorType.INVALID_ARGUMENT));
    ListenableFuture<Object> failedFuture = Futures.immediateFailedFuture(remoteException);
    assertThatThrownBy(() -> DefaultClients.INSTANCE.block(failedFuture)).isInstanceOf(RemoteException.class).hasFieldOrPropertyWithValue("status", ErrorType.INVALID_ARGUMENT.httpErrorCode());
}
Also used : ServiceException(com.palantir.conjure.java.api.errors.ServiceException) RemoteException(com.palantir.conjure.java.api.errors.RemoteException) UnknownRemoteException(com.palantir.conjure.java.api.errors.UnknownRemoteException) Test(org.junit.jupiter.api.Test)

Example 14 with RemoteException

use of com.palantir.conjure.java.api.errors.RemoteException in project dialogue by palantir.

the class ErrorDecoderTest method testSpecificException.

@Test
public void testSpecificException() {
    RemoteException exception = encodeAndDecode(new IllegalArgumentException("msg"));
    assertThat(exception).isInstanceOf(RemoteException.class);
    assertThat(exception.getMessage()).startsWith("RemoteException: java.lang.IllegalArgumentException (msg)");
}
Also used : RemoteException(com.palantir.conjure.java.api.errors.RemoteException) UnknownRemoteException(com.palantir.conjure.java.api.errors.UnknownRemoteException) Test(org.junit.jupiter.api.Test)

Example 15 with RemoteException

use of com.palantir.conjure.java.api.errors.RemoteException in project conjure-java-runtime by palantir.

the class SingleParamServicesTest method runTestCase.

private void runTestCase(ImmutableMap<String, Object> servicesMaps) throws Exception {
    Assume.assumeFalse(Cases.shouldIgnoreJersey(endpointName, jsonString));
    log.info("Invoking {} {}({})", serviceName, endpointName, jsonString);
    Object service = servicesMaps.get(serviceName);
    for (Method method : servicesMaps.get(serviceName).getClass().getMethods()) {
        String name = method.getName();
        if (method.getParameterCount() == 1) {
            // conjure-java generates `default` methods for optional query params, we don't want to call these
            continue;
        }
        if (endpointName.get().equals(name)) {
            try {
                // HACKHACK, index parameter order is different for different services.
                if ("singleHeaderService".equals(serviceName)) {
                    Type type = method.getGenericParameterTypes()[0];
                    Class<?> cls = ClassUtils.getClass(type.getTypeName());
                    method.invoke(service, jsonMapper.readValue(jsonString, cls), index);
                } else {
                    Type type = method.getGenericParameterTypes()[1];
                    Class<?> cls = ClassUtils.getClass(type.getTypeName());
                    method.invoke(service, index, jsonMapper.readValue(jsonString, cls));
                }
                log.info("Successfully post param to endpoint {} and index {}", endpointName, index);
            } catch (RemoteException e) {
                log.error("Caught exception with params: {}", e.getError().parameters(), e);
                throw e;
            }
        }
    }
}
Also used : Type(java.lang.reflect.Type) Method(java.lang.reflect.Method) RemoteException(com.palantir.conjure.java.api.errors.RemoteException)

Aggregations

RemoteException (com.palantir.conjure.java.api.errors.RemoteException)22 Test (org.junit.jupiter.api.Test)11 SerializableError (com.palantir.conjure.java.api.errors.SerializableError)7 ServiceException (com.palantir.conjure.java.api.errors.ServiceException)7 Test (org.junit.Test)6 UnknownRemoteException (com.palantir.conjure.java.api.errors.UnknownRemoteException)5 Response (okhttp3.Response)5 TypeMarker (com.palantir.conjure.java.undertow.lib.TypeMarker)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Method (java.lang.reflect.Method)3 Type (java.lang.reflect.Type)3 FutureCallback (com.google.common.util.concurrent.FutureCallback)2 BodySerDe (com.palantir.dialogue.BodySerDe)2 TestResponse (com.palantir.dialogue.TestResponse)2 SafeIoException (com.palantir.logsafe.exceptions.SafeIoException)2 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 Call (okhttp3.Call)2 Callback (okhttp3.Callback)2 MockResponse (okhttp3.mockwebserver.MockResponse)2