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());
}
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");
}
}
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");
}
}
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);
}
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"));
}
Aggregations