Skip to content

Commit

Permalink
fix(NET): NET SDK Error Wrapping (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
josecorella authored Jul 7, 2023
1 parent 73d7de4 commit d7b080a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ public TokenTree generateOperationShim(final ShapeId operationShapeId) {

final String baseExceptionForService = nameResolver.qualifiedClassForBaseServiceException();
final TokenTree catchBlock = Token.of("""
catch (System.AggregateException aggregate) when (aggregate.InnerException is %s ex) {
return %s.create_Failure(TypeConversion.ToDafny_CommonError(ex));
catch (System.AggregateException aggregate) {
return %s.create_Failure(TypeConversion.ToDafny_CommonError(aggregate.InnerException));
}
""".formatted(
baseExceptionForService,
dafnyOutputType));

final TokenTree methodSignature = generateOperationShimSignature(operationShape);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ public TokenTree generateSpecificExceptionClass(final StructureShape structureSh
// TODO Need to extend for a common class for this namespace
final TokenTree classHeader = Token.of("public class %s : Exception".formatted(exceptionName));
// TODO need to model _all_ possible members here...
final TokenTree messageCtor = Token.of("public %s(string message) : base(message) {}".formatted(exceptionName));
final TokenTree messageCtor = Token.of("""
public %s(string message) : base(message) {}
public string getMessage() { return this.Message; }""".formatted(exceptionName));
return TokenTree.of(classHeader, messageCtor.braced()).namespaced(Token.of(nameResolver.namespaceForService()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@
import com.google.common.annotations.VisibleForTesting;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -47,6 +40,21 @@
*/
public class TypeConversionCodegen {
public static final String C_SHARP_SYSTEM_EXCEPTION = "System.Exception";
// Edge case for constructors of Exceptions that extend the base Exception class.
public static final List<String> ERROR_CTOR = Arrays.asList(
"AwsCryptographicMaterialProvidersException",
"EntryAlreadyExists",
"EntryDoesNotExist",
"InvalidAlgorithmSuiteInfo",
"InvalidAlgorithmSuiteInfoOnDecrypt",
"InvalidAlgorithmSuiteInfoOnEncrypt",
"InvalidDecryptionMaterials",
"InvalidDecryptionMaterialsTransition",
"InvalidEncryptionMaterials",
"InvalidEncryptionMaterialsTransition",
"KeyStoreException",
"KeyVectorException"
);

/**
* A pair of type converter methods that converts between the compiled Dafny representation and the idiomatic C#
Expand Down Expand Up @@ -461,6 +469,10 @@ private String generateConstructorArg(final MemberShape memberShape) {
typeConverterForShape(memberShape.getId(), TO_DAFNY),
nameResolver.classPropertyForStructureMember(memberShape));
}
if (ERROR_CTOR.contains(memberShape.getContainer().getName()) || memberShape.getContainer().getName().endsWith("Error")) {
return "%s(value.getMessage())".formatted(
typeConverterForShape(memberShape.getId(), TO_DAFNY));
}
return "%s(value.%s)".formatted(
typeConverterForShape(memberShape.getId(), TO_DAFNY),
nameResolver.classPropertyForStructureMember(memberShape));
Expand Down Expand Up @@ -546,6 +558,7 @@ public TypeConverter generateUnionConverter(final UnionShape unionShape) {
.of(defNames
.stream()
.map(memberShape -> {
final String propertyNameEdgeCase = StringUtils.equals(nameResolver.classPropertyForStructureMember(memberShape), "KmsKeyArn") ? "kmsKeyArn" : nameResolver.classPropertyForStructureMember(memberShape);
final String propertyName = nameResolver.classPropertyForStructureMember(memberShape);
final String memberFromDafnyConverterName = typeConverterForShape(
memberShape.getId(), FROM_DAFNY);
Expand All @@ -556,7 +569,7 @@ public TypeConverter generateUnionConverter(final UnionShape unionShape) {
destructorValue = DafnyNameResolverHelpers.dafnyCompilesExtra_(memberShape.getMemberName());
}
return TokenTree
.of("if (value.is_%s)".formatted(propertyName))
.of("if (value.is_%s)".formatted(propertyNameEdgeCase))
.append(TokenTree
.of(
"converted.%s = %s(concrete.dtor_%s);"
Expand Down Expand Up @@ -1136,7 +1149,7 @@ public TypeConverter generateSpecificExceptionConverter(final StructureShape err
assert errorShape.getMember("message").isPresent();
final ShapeId messageShapeId = errorShape.getId().withMember("message");

final Token fromDafnyBody = Token.of("return new %s(%s(value.message));".formatted(
final Token fromDafnyBody = Token.of("return new %s(%s(value.Message));".formatted(
nameResolver.baseTypeForShape(errorShape.getId()),
typeConverterForShape(messageShapeId, FROM_DAFNY)
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ public void testGenerateOperationShim() {
this._impl.GoAsync(sdkRequest).Result;
return Wrappers_Compile.Result<%1$s>.create_Success(%3$s(sdkResponse));
}
catch (System.AggregateException aggregate)
when (aggregate.InnerException is Amazon.FoobarService.AmazonFoobarServiceException ex) {
return Wrappers_Compile.Result<%1$s>.create_Failure(TypeConversion.ToDafny_CommonError(ex));
catch (System.AggregateException aggregate) {
return Wrappers_Compile.Result<%1$s>.create_Failure(TypeConversion.ToDafny_CommonError(aggregate.InnerException));
}
}
""".formatted(resultTypeParams, requestFromDafnyConverter, responseToDafnyConverter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ public void testGenerateSpecificExceptionClass() {
namespace Test.Foobar {
public class UnfortunateException : Exception {
public UnfortunateException(string message) : base(message) {}
public string getMessage() { return this.Message;}
}
}
""");
Expand Down

0 comments on commit d7b080a

Please sign in to comment.