Skip to content

Commit

Permalink
fix: create ErrorStringResponse object and pass ErrorT type to the Si…
Browse files Browse the repository at this point in the history
…mpleEndpoint request (opensearch-project#301)

Signed-off-by: Dominik Szczepanczyk <[email protected]>
  • Loading branch information
szczepanczykd committed Mar 7, 2023
1 parent 89acf69 commit e79ce60
Show file tree
Hide file tree
Showing 153 changed files with 421 additions and 186 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

//----------------------------------------------------
// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST.
//----------------------------------------------------

package org.opensearch.client.opensearch._types;

import jakarta.json.stream.JsonGenerator;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;
import org.opensearch.client.util.ObjectBuilderBase;

import java.util.function.Function;

// typedef: _types.ErrorSimpleResponseBase

/**
* The response returned by Elasticsearch 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");

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.client.opensearch._types;

public class OpenSearchExceptionFactory {

private OpenSearchExceptionFactory() {
//should be empty
}

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)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,5 @@ public AliasesRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, AliasesResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, AliasesResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,5 @@ public AllocationRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, AllocationResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, AllocationResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ClusterManagerRequest() {
params.put("format", "json");
return params;

}, SimpleEndpoint.emptyMap(), false, ClusterManagerResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, ClusterManagerResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);

/**
* Endpoint "{@code cat.cluster_manager}".
Expand All @@ -109,5 +109,5 @@ public ClusterManagerRequest() {
params.put("format", "json");
return params;

}, SimpleEndpoint.emptyMap(), false, ClusterManagerResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, ClusterManagerResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,5 @@ public CountRequest build() {
params.put("format", "json");
return params;

}, SimpleEndpoint.emptyMap(), false, CountResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, CountResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,5 @@ public FielddataRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, FielddataResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, FielddataResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,5 @@ public HealthRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, HealthResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, HealthResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ public HelpRequest() {
params.put("format", "json");
return params;

}, SimpleEndpoint.emptyMap(), false, HelpResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, HelpResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,5 +349,5 @@ public IndicesRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, IndicesResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, IndicesResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ public NodeattrsRequest() {
params.put("format", "json");
return params;

}, SimpleEndpoint.emptyMap(), false, NodeattrsResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, NodeattrsResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,5 @@ public NodesRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, NodesResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, NodesResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ public PendingTasksRequest() {
params.put("format", "json");
return params;

}, SimpleEndpoint.emptyMap(), false, PendingTasksResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, PendingTasksResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ public PluginsRequest() {
params.put("format", "json");
return params;

}, SimpleEndpoint.emptyMap(), false, PluginsResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, PluginsResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,5 @@ public RecoveryRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, RecoveryResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, RecoveryResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ public RepositoriesRequest() {
params.put("format", "json");
return params;

}, SimpleEndpoint.emptyMap(), false, RepositoriesResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, RepositoriesResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,5 @@ public SegmentsRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, SegmentsResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, SegmentsResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,5 @@ public ShardsRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, ShardsResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, ShardsResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,5 @@ public SnapshotsRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, SnapshotsResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, SnapshotsResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,5 @@ public TasksRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, TasksResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, TasksResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,5 @@ public TemplatesRequest build() {
params.put("format", "json");
return params;

}, SimpleEndpoint.emptyMap(), false, TemplatesResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, TemplatesResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,5 @@ public ThreadPoolRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, ThreadPoolResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, ThreadPoolResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,5 @@ protected static void setupAllocationExplainRequestDeserializer(
}
return params;

}, SimpleEndpoint.emptyMap(), true, AllocationExplainResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), true, AllocationExplainResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,5 @@ public ClusterStatsRequest build() {
}
return params;

}, SimpleEndpoint.emptyMap(), false, ClusterStatsResponse._DESERIALIZER);
}, SimpleEndpoint.emptyMap(), false, ClusterStatsResponse._DESERIALIZER, ErrorResponse._DESERIALIZER);
}
Loading

0 comments on commit e79ce60

Please sign in to comment.