Skip to content

Commit

Permalink
refactor: adjusted code after review (opensearch-project#301)
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Szczepanczyk <szczepanczyk.dominik@gmail.com>
  • Loading branch information
szczepanczykd committed Mar 30, 2023
1 parent a94cd3c commit 89999ac
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,8 @@ public ErrorResponse build() {
ErrorResponse::setupErrorResponseDeserializer);

protected static void setupErrorResponseDeserializer(ObjectDeserializer<ErrorResponse.Builder> op) {

op.add(Builder::error, ErrorCause._DESERIALIZER, "error");
op.add(Builder::status, JsonpDeserializer.integerDeserializer(), "status");

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Builder, ObjectBuilder<ErrorStringResponse>> 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<ErrorStringResponse> {
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<ErrorCause.Builder, ObjectBuilder<String>> 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<ErrorStringResponse> _DESERIALIZER = ObjectBuilderDeserializer.lazy(Builder::new,
ErrorStringResponse::setupErrorResponseDeserializer);

protected static void setupErrorResponseDeserializer(ObjectDeserializer<ErrorStringResponse.Builder> 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<Builder, ObjectBuilder<ErrorStringResponse>> 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<ErrorStringResponse> {
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<ErrorCause.Builder, ObjectBuilder<String>> 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<ErrorStringResponse> _DESERIALIZER = ObjectBuilderDeserializer.lazy(Builder::new,
ErrorStringResponse::setupErrorResponseDeserializer);

protected static void setupErrorResponseDeserializer(ObjectDeserializer<ErrorStringResponse.Builder> op) {
op.add(Builder::error, JsonpDeserializer.stringDeserializer(), "error");
op.add(Builder::status, JsonpDeserializer.integerDeserializer(), "status");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ErrorT> 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 <ErrorT> 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)
)
);
}
}
Loading

0 comments on commit 89999ac

Please sign in to comment.