Skip to content

Commit

Permalink
Add Operation helper method to include service errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Sep 24, 2021
1 parent 039c5cf commit f60c17e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
Expand Down Expand Up @@ -189,7 +189,7 @@ public List<StructureShape> getErrors(ToShapeId operation) {
* @return Returns the list of error structures, or an empty list.
*/
public List<StructureShape> getErrors(ToShapeId service, ToShapeId operation) {
Set<StructureShape> result = new TreeSet<>(getErrors(service));
Set<StructureShape> result = new LinkedHashSet<>(getErrors(service));
result.addAll(getErrors(operation));
return new ArrayList<>(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import software.amazon.smithy.model.knowledge.OperationIndex;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.ToSmithyBuilder;

Expand Down Expand Up @@ -79,18 +82,43 @@ public Optional<ShapeId> getOutput() {
}

/**
* <p>Gets a list of the error shape IDs that can be encountered.</p>
* <p>Gets a list of the error shape IDs bound directly to the operation
* that can be encountered.
*
* <p>This DOES NOT include errors that are common to a service. Operations
* can be bound to multiple services, so common service errors cannot be
* returned by this method. Use {@link #getErrors(ServiceShape)} or
* {@link OperationIndex#getErrors(ToShapeId, ToShapeId)} to get all of the
* errors an operation can encounter when used within a service.</p>
*
* <p>Each returned {@link ShapeId} must resolve to a
* {@link StructureShape} that is targeted by an error trait; however,
* this is only guaranteed after a model is validated.</p>
*
* @return Returns the errors.
* @see #getErrors(ServiceShape)
* @see OperationIndex#getErrors(ToShapeId, ToShapeId)
*/
public List<ShapeId> getErrors() {
return errors;
}

/**
* <p>Gets a list of the error shape IDs the operation can encounter,
* including any common errors of a service.
*
* <p>No validation is performed here to ensure that the operation is
* actually bound to the given service shape.
*
* @return Returns the errors.
* @see OperationIndex#getErrors(ToShapeId, ToShapeId)
*/
public List<ShapeId> getErrors(ServiceShape service) {
Set<ShapeId> result = new LinkedHashSet<>(service.getErrors());
result.addAll(getErrors());
return new ArrayList<>(result);
}

@Override
public boolean equals(Object other) {
if (!super.equals(other)) {
Expand All @@ -109,7 +137,7 @@ public boolean equals(Object other) {
public static final class Builder extends AbstractShapeBuilder<Builder, OperationShape> {
private ShapeId input;
private ShapeId output;
private List<ShapeId> errors = new ArrayList<>();
private final List<ShapeId> errors = new ArrayList<>();

@Override
public ShapeType getShapeType() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

package software.amazon.smithy.model.shapes;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;

import java.util.List;
import org.junit.jupiter.api.Test;

public class OperationShapeTest {
@Test
public void combinesErrorsWithServiceErrors() {
ServiceShape service = ServiceShape.builder()
.id("com.foo#Example")
.version("x")
.addError("com.foo#Common1")
.addError(ShapeId.from("com.foo#Common2"))
.build();

OperationShape operation = OperationShape.builder()
.id("com.foo#Operation")
.addError("com.foo#OperationError")
.build();

List<ShapeId> allErrors = operation.getErrors(service);

assertThat(allErrors, contains(
ShapeId.from("com.foo#Common1"),
ShapeId.from("com.foo#Common2"),
ShapeId.from("com.foo#OperationError")));
}
}

0 comments on commit f60c17e

Please sign in to comment.