From 89999ace3bfdf9357a3b1720f63705c2b8e67d82 Mon Sep 17 00:00:00 2001 From: Dominik Szczepanczyk Date: Thu, 30 Mar 2023 19:48:27 +0200 Subject: [PATCH] refactor: adjusted code after review (#301) Signed-off-by: Dominik Szczepanczyk --- .../opensearch/_types/ErrorResponse.java | 2 - .../_types/ErrorStringResponse.java | 225 +++++++++--------- .../_types/OpenSearchExceptionFactory.java | 44 ++-- .../types/OpenSearchExceptionFactoryTest.java | 93 ++++---- 4 files changed, 171 insertions(+), 193 deletions(-) diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java index 2f71c65905..79e3f8ec78 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorResponse.java @@ -163,10 +163,8 @@ public ErrorResponse build() { ErrorResponse::setupErrorResponseDeserializer); protected static void setupErrorResponseDeserializer(ObjectDeserializer op) { - op.add(Builder::error, ErrorCause._DESERIALIZER, "error"); op.add(Builder::status, JsonpDeserializer.integerDeserializer(), "status"); - } } diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorStringResponse.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorStringResponse.java index cfb62a9e55..1b43a33d0f 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorStringResponse.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/ErrorStringResponse.java @@ -6,11 +6,6 @@ * compatible open source license. */ -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - package org.opensearch.client.opensearch._types; import jakarta.json.stream.JsonGenerator; @@ -26,123 +21,119 @@ import java.util.function.Function; -// typedef: _types.ErrorSimpleResponseBase - /** * The response returned by OpenSearch when request execution did not * succeed. */ @JsonpDeserializable public class ErrorStringResponse implements JsonpSerializable { - private final String error; - - private final int status; - - // --------------------------------------------------------------------------------------------- - - private ErrorStringResponse(Builder builder) { - - this.error = ApiTypeHelper.requireNonNull(builder.error, this, "error"); - this.status = ApiTypeHelper.requireNonNull(builder.status, this, "status"); - - } - - public static ErrorStringResponse of(Function> fn) { - return fn.apply(new Builder()).build(); - } - - /** - * Required - API name: {@code error} - */ - public final String error() { - return this.error; - } - - /** - * Required - API name: {@code status} - */ - public final int status() { - return this.status; - } - - /** - * Serialize this object to JSON. - */ - public void serialize(JsonGenerator generator, JsonpMapper mapper) { - generator.writeStartObject(); - serializeInternal(generator, mapper); - generator.writeEnd(); - } - - protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { - - generator.writeKey("error"); - generator.write(this.error); - - generator.writeKey("status"); - generator.write(this.status); - - } - - // --------------------------------------------------------------------------------------------- - - /** - * Builder for {@link ErrorStringResponse}. - */ - - public static class Builder extends ObjectBuilderBase implements ObjectBuilder { - private String error; - - private Integer status; - - /** - * Required - API name: {@code error} - */ - public final Builder error(String value) { - this.error = value; - return this; - } - - /** - * Required - API name: {@code error} - */ - public final Builder error(Function> fn) { - return this.error(fn.apply(new ErrorCause.Builder()).build()); - } - - /** - * Required - API name: {@code status} - */ - public final Builder status(int value) { - this.status = value; - return this; - } - - /** - * Builds a {@link ErrorStringResponse}. - * - * @throws NullPointerException if some of the required fields are null. - */ - public ErrorStringResponse build() { - _checkSingleUse(); - - return new ErrorStringResponse(this); - } - } - - // --------------------------------------------------------------------------------------------- - - /** - * Json deserializer for {@link ErrorStringResponse} - */ - public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer.lazy(Builder::new, - ErrorStringResponse::setupErrorResponseDeserializer); - - protected static void setupErrorResponseDeserializer(ObjectDeserializer op) { - - op.add(Builder::error, JsonpDeserializer.stringDeserializer(), "error"); - op.add(Builder::status, JsonpDeserializer.integerDeserializer(), "status"); - - } + private final String error; + + private final int status; + + // --------------------------------------------------------------------------------------------- + + private ErrorStringResponse(Builder builder) { + + this.error = ApiTypeHelper.requireNonNull(builder.error, this, "error"); + this.status = ApiTypeHelper.requireNonNull(builder.status, this, "status"); + + } + + public static ErrorStringResponse of(Function> fn) { + return fn.apply(new Builder()).build(); + } + + /** + * Required - API name: {@code error} + */ + public final String error() { + return this.error; + } + + /** + * Required - API name: {@code status} + */ + public final int status() { + return this.status; + } + + /** + * Serialize this object to JSON. + */ + public void serialize(JsonGenerator generator, JsonpMapper mapper) { + generator.writeStartObject(); + serializeInternal(generator, mapper); + generator.writeEnd(); + } + + protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { + + generator.writeKey("error"); + generator.write(this.error); + + generator.writeKey("status"); + generator.write(this.status); + + } + + // --------------------------------------------------------------------------------------------- + + /** + * Builder for {@link ErrorStringResponse}. + */ + + public static class Builder extends ObjectBuilderBase implements ObjectBuilder { + private String error; + + private Integer status; + + /** + * Required - API name: {@code error} + */ + public final Builder error(String value) { + this.error = value; + return this; + } + + /** + * Required - API name: {@code error} + */ + public final Builder error(Function> fn) { + return this.error(fn.apply(new ErrorCause.Builder()).build()); + } + + /** + * Required - API name: {@code status} + */ + public final Builder status(int value) { + this.status = value; + return this; + } + + /** + * Builds a {@link ErrorStringResponse}. + * + * @throws NullPointerException if some of the required fields are null. + */ + public ErrorStringResponse build() { + _checkSingleUse(); + + return new ErrorStringResponse(this); + } + } + + // --------------------------------------------------------------------------------------------- + + /** + * Json deserializer for {@link ErrorStringResponse} + */ + public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer.lazy(Builder::new, + ErrorStringResponse::setupErrorResponseDeserializer); + + protected static void setupErrorResponseDeserializer(ObjectDeserializer op) { + op.add(Builder::error, JsonpDeserializer.stringDeserializer(), "error"); + op.add(Builder::status, JsonpDeserializer.integerDeserializer(), "status"); + } } diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/OpenSearchExceptionFactory.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/OpenSearchExceptionFactory.java index 886a8f38c5..3ea4641e80 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/OpenSearchExceptionFactory.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/OpenSearchExceptionFactory.java @@ -6,35 +6,29 @@ * compatible open source license. */ -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - package org.opensearch.client.opensearch._types; public class OpenSearchExceptionFactory { - private OpenSearchExceptionFactory() { - //should be empty - } + private OpenSearchExceptionFactory() { + } - public static OpenSearchException createException(ErrorT error) { - if (error instanceof ErrorResponse) { - return new OpenSearchException((ErrorResponse) error); - } else if (error instanceof ErrorStringResponse) { - ErrorStringResponse errorStringResponse = (ErrorStringResponse) error; - return new OpenSearchException(getErrorResponse(errorStringResponse.status(), "string_error", errorStringResponse.error())); - } else { - throw new OpenSearchException(getErrorResponse(500, "error_type", "Unknown error type: " + error.getClass())); - } - } + public static OpenSearchException createException(ErrorT error) { + if (error instanceof ErrorResponse) { + return new OpenSearchException((ErrorResponse) error); + } else if (error instanceof ErrorStringResponse) { + ErrorStringResponse errorStringResponse = (ErrorStringResponse) error; + return new OpenSearchException(getErrorResponse(errorStringResponse.status(), "string_error", errorStringResponse.error())); + } else { + throw new OpenSearchException(getErrorResponse(500, "error_type", "Unknown error type: " + error.getClass())); + } + } - private static ErrorResponse getErrorResponse(int status, String type, String reason) { - return ErrorResponse.of( - builder -> builder.status(status).error( - builder1 -> builder1.type(type).reason(reason) - ) - ); - } + private static ErrorResponse getErrorResponse(int status, String type, String reason) { + return ErrorResponse.of( + builder -> builder.status(status).error( + builder1 -> builder1.type(type).reason(reason) + ) + ); + } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/types/OpenSearchExceptionFactoryTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/types/OpenSearchExceptionFactoryTest.java index aea5908cac..c95320ef51 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/types/OpenSearchExceptionFactoryTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/types/OpenSearchExceptionFactoryTest.java @@ -6,11 +6,6 @@ * compatible open source license. */ -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - package org.opensearch.client.opensearch.types; import jakarta.json.stream.JsonGenerator; @@ -28,52 +23,52 @@ public class OpenSearchExceptionFactoryTest extends Assert { - @Test - public void shouldCreateExceptionWithErrorResponseDetails() { - ErrorResponse errorResponse = ErrorResponse.of(builder -> builder.status(404) - .error(error -> error.type("exception_type") - .reason("exception_reason") - .metadata(Map.of("meta", JsonData.of("data"))) - ) - ); - OpenSearchException exception = OpenSearchExceptionFactory.createException(errorResponse); - assertEquals(404, exception.status()); - assertEquals("exception_type", exception.error().type()); - assertEquals("exception_reason", exception.error().reason()); - assertTrue(exception.error().metadata().containsKey("meta")); - assertEquals("data", exception.error().metadata().get("meta").toString()); - } + @Test + public void shouldCreateExceptionWithErrorResponseDetails() { + ErrorResponse errorResponse = ErrorResponse.of(builder -> builder.status(404) + .error(error -> error.type("exception_type") + .reason("exception_reason") + .metadata(Map.of("meta", JsonData.of("data"))) + ) + ); + OpenSearchException exception = OpenSearchExceptionFactory.createException(errorResponse); + assertEquals(404, exception.status()); + assertEquals("exception_type", exception.error().type()); + assertEquals("exception_reason", exception.error().reason()); + assertTrue(exception.error().metadata().containsKey("meta")); + assertEquals("data", exception.error().metadata().get("meta").toString()); + } - @Test - public void shouldCreateExceptionWithErrorStringResponseDetails() { - ErrorStringResponse errorResponse = ErrorStringResponse.of(builder -> builder.status(404).error("exception_string_reason")); + @Test + public void shouldCreateExceptionWithErrorStringResponseDetails() { + ErrorStringResponse errorResponse = ErrorStringResponse.of(builder -> builder.status(404).error("exception_string_reason")); - OpenSearchException exception = OpenSearchExceptionFactory.createException(errorResponse); - assertEquals(404, exception.status()); - assertEquals("string_error", exception.error().type()); - assertEquals("exception_string_reason", exception.error().reason()); - } + OpenSearchException exception = OpenSearchExceptionFactory.createException(errorResponse); + assertEquals(404, exception.status()); + assertEquals("string_error", exception.error().type()); + assertEquals("exception_string_reason", exception.error().reason()); + } - @Test - public void shouldThrowExceptionWhenErrorTypeCannotBeHandled() { - FakeErrorResponse fakeErrorResponse = new FakeErrorResponse(); - try { - OpenSearchExceptionFactory.createException(fakeErrorResponse); - fail(); - } catch (OpenSearchException ex) { - assertEquals(500, ex.status()); - assertEquals("error_type", ex.error().type()); - assertEquals("Unknown error type: " + fakeErrorResponse.getClass(), ex.error().reason()); - } - } + @Test + public void shouldThrowExceptionWhenErrorTypeCannotBeHandled() { + FakeErrorResponse fakeErrorResponse = new FakeErrorResponse(); + try { + OpenSearchExceptionFactory.createException(fakeErrorResponse); + fail(); + } catch (OpenSearchException ex) { + assertEquals(500, ex.status()); + assertEquals("error_type", ex.error().type()); + assertEquals("Unknown error type: " + fakeErrorResponse.getClass(), ex.error().reason()); + } + } - private class FakeErrorResponse implements JsonpSerializable { - @Override - public void serialize(JsonGenerator generator, JsonpMapper mapper) { - generator.writeStartObject(); - generator.writeKey("error"); - generator.write("fakeError"); - generator.writeEnd(); - } - } + private class FakeErrorResponse implements JsonpSerializable { + @Override + public void serialize(JsonGenerator generator, JsonpMapper mapper) { + generator.writeStartObject(); + generator.writeKey("error"); + generator.write("fakeError"); + generator.writeEnd(); + } + } }