Search in sources :

Example 1 with SerializableError

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

the class ConjureExceptionHandlerTest method handles401RemoteException.

@Test
public void handles401RemoteException() throws IOException {
    SerializableError remoteError = SerializableError.forException(new ServiceException(ErrorType.create(Code.UNAUTHORIZED, "Test:ErrorName"), SafeArg.of("foo", "bar")));
    exception = new RemoteException(remoteError, ErrorType.UNAUTHORIZED.httpErrorCode());
    Response response = execute();
    // Propagates errorInstanceId and does not change error code and name
    // Does not propagate args
    SerializableError expectedPropagatedError = SerializableError.builder().errorCode(ErrorType.UNAUTHORIZED.code().toString()).errorName("Test:ErrorName").errorInstanceId(remoteError.errorInstanceId()).build();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Encodings.json().serializer(new TypeMarker<SerializableError>() {
    }).serialize(expectedPropagatedError, stream);
    assertThat(response.body().string()).isEqualTo(stream.toString(StandardCharsets.UTF_8));
    assertThat(response.code()).isEqualTo(ErrorType.UNAUTHORIZED.httpErrorCode());
}
Also used : Response(okhttp3.Response) ServiceException(com.palantir.conjure.java.api.errors.ServiceException) SerializableError(com.palantir.conjure.java.api.errors.SerializableError) TypeMarker(com.palantir.conjure.java.undertow.lib.TypeMarker) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RemoteException(com.palantir.conjure.java.api.errors.RemoteException) Test(org.junit.jupiter.api.Test)

Example 2 with SerializableError

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

the class UndertowServiceEteTest method java_url_client_receives_unprocessable_entity_with_null_body.

@Test
public void java_url_client_receives_unprocessable_entity_with_null_body() throws IOException {
    HttpURLConnection httpUrlConnection = preparePostRequest();
    httpUrlConnection.setRequestProperty("Authorization", "Bearer authheader");
    sendPostRequestData(httpUrlConnection, "");
    assertThat(httpUrlConnection.getResponseCode()).isEqualTo(422);
    try (InputStream responseBody = httpUrlConnection.getErrorStream()) {
        SerializableError error = CLIENT_OBJECT_MAPPER.readValue(responseBody, SerializableError.class);
        assertThat(error.errorCode()).isEqualTo("INVALID_ARGUMENT");
        assertThat(error.errorName()).isEqualTo("Conjure:UnprocessableEntity");
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) SerializableError(com.palantir.conjure.java.api.errors.SerializableError) Test(org.junit.jupiter.api.Test)

Example 3 with SerializableError

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

the class UndertowServiceEteTest method java_url_client_receives_bad_request_with_json_null.

@Test
public void java_url_client_receives_bad_request_with_json_null() throws IOException {
    HttpURLConnection connection = preparePostRequest();
    connection.setRequestProperty("Authorization", "Bearer authheader");
    connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/json");
    connection.setRequestProperty(HttpHeaders.ACCEPT, "application/json");
    sendPostRequestData(connection, "null");
    assertThat(connection.getResponseCode()).isEqualTo(400);
    try (InputStream responseBody = connection.getErrorStream()) {
        SerializableError error = CLIENT_OBJECT_MAPPER.readValue(responseBody, SerializableError.class);
        assertThat(error.errorCode()).isEqualTo("INVALID_ARGUMENT");
        assertThat(error.errorName()).isEqualTo("Default:InvalidArgument");
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) SerializableError(com.palantir.conjure.java.api.errors.SerializableError) Test(org.junit.jupiter.api.Test)

Example 4 with SerializableError

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

the class RemoteExceptionAssertTest method testSanity.

@Test
public void testSanity() throws Exception {
    ErrorType actualType = ErrorType.FAILED_PRECONDITION;
    SerializableError error = SerializableError.forException(new ServiceException(actualType));
    Assertions.assertThat(new RemoteException(error, actualType.httpErrorCode())).isGeneratedFromErrorType(actualType);
    assertThatThrownBy(() -> Assertions.assertThat(new RemoteException(error, actualType.httpErrorCode() + 1)).isGeneratedFromErrorType(actualType)).isInstanceOf(AssertionError.class).hasMessage("Expected error status to be %s, but found %s", actualType.httpErrorCode(), actualType.httpErrorCode() + 1);
}
Also used : ErrorType(com.palantir.conjure.java.api.errors.ErrorType) ServiceException(com.palantir.conjure.java.api.errors.ServiceException) SerializableError(com.palantir.conjure.java.api.errors.SerializableError) RemoteException(com.palantir.conjure.java.api.errors.RemoteException) Test(org.junit.jupiter.api.Test)

Example 5 with SerializableError

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

the class ExceptionMappingTest method testServiceException.

@Test
public void testServiceException() {
    Response response = target.path("throw-service-exception").request().get();
    assertThat(response.getStatus()).isEqualTo(ErrorType.INVALID_ARGUMENT.httpErrorCode());
    SerializableError error = response.readEntity(SerializableError.class);
    assertThat(error.errorCode()).isEqualTo(ErrorType.INVALID_ARGUMENT.code().toString());
    assertThat(error.errorName()).isEqualTo(ErrorType.INVALID_ARGUMENT.name());
    assertThat(error.parameters()).containsExactlyInAnyOrderEntriesOf(ImmutableMap.of("arg", "value"));
}
Also used : Response(javax.ws.rs.core.Response) SerializableError(com.palantir.conjure.java.api.errors.SerializableError) Test(org.junit.Test)

Aggregations

SerializableError (com.palantir.conjure.java.api.errors.SerializableError)35 Test (org.junit.Test)21 Response (javax.ws.rs.core.Response)20 Test (org.junit.jupiter.api.Test)13 RemoteException (com.palantir.conjure.java.api.errors.RemoteException)7 ServiceException (com.palantir.conjure.java.api.errors.ServiceException)7 Response (okhttp3.Response)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Request (okhttp3.Request)4 TypeMarker (com.palantir.conjure.java.undertow.lib.TypeMarker)3 JerseyTest (org.glassfish.jersey.test.JerseyTest)3 BodySerDe (com.palantir.dialogue.BodySerDe)2 TestResponse (com.palantir.dialogue.TestResponse)2 InputStream (java.io.InputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 JsonMapper (com.fasterxml.jackson.databind.json.JsonMapper)1 ErrorType (com.palantir.conjure.java.api.errors.ErrorType)1 Call (okhttp3.Call)1 OkHttpClient (okhttp3.OkHttpClient)1