Skip to content

Commit 704ea10

Browse files
authored
refactor!: Update codegen to generate base exception rather than UnknownServiceErrorException (#484)
1 parent e23c843 commit 704ea10

File tree

5 files changed

+130
-20
lines changed

5 files changed

+130
-20
lines changed

aws-runtime/aws-core/common/src/aws/sdk/kotlin/runtime/Exceptions.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,3 @@ public open class ConfigurationException : ClientException {
7979

8080
public constructor(cause: Throwable?) : super(cause)
8181
}
82-
83-
/**
84-
* An exception that is thrown when the returned (error) response is not known by this version of the SDK
85-
* (i.e. the modeled error code is not known)
86-
*/
87-
public class UnknownServiceErrorException : AwsServiceException {
88-
89-
public constructor() : super()
90-
91-
public constructor(message: String?) : super(message)
92-
93-
public constructor(message: String?, cause: Throwable?) : super(message, cause)
94-
95-
public constructor(cause: Throwable?) : super(cause)
96-
}

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/AwsRuntimeTypes.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ object AwsRuntimeTypes {
2121
val AwsClientOption = runtimeSymbol("AwsClientOption", AwsKotlinDependency.AWS_CORE, "client")
2222
val AuthAttributes = runtimeSymbol("AuthAttributes", AwsKotlinDependency.AWS_CORE, "execution")
2323
val AwsErrorMetadata = runtimeSymbol("AwsErrorMetadata", AwsKotlinDependency.AWS_CORE)
24-
val UnknownServiceErrorException = runtimeSymbol("UnknownServiceErrorException", AwsKotlinDependency.AWS_CORE)
2524
val ClientException = runtimeSymbol("ClientException", AwsKotlinDependency.AWS_CORE)
2625
}
2726

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/s3/S3Generator.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class S3Generator : RestXml() {
6565
exceptionBaseSymbol,
6666
RuntimeTypes.Http.readAll,
6767
RuntimeTypes.Http.StatusCode,
68-
AwsRuntimeTypes.Core.UnknownServiceErrorException,
6968
AwsRuntimeTypes.Http.withPayload,
7069
s3ErrorDetails,
7170
setS3ErrorMetadata,
@@ -92,7 +91,7 @@ class S3Generator : RestXml() {
9291
}
9392
.dedent()
9493
.withBlock("} catch (ex: Exception) {", "}") {
95-
withBlock("""throw #T("failed to parse response as ${ctx.protocol.name} error", ex).also {""", "}", AwsRuntimeTypes.Core.UnknownServiceErrorException) {
94+
withBlock("""throw #T("Failed to parse response as '${ctx.protocol.name}' error", ex).also {""", "}", exceptionBaseSymbol) {
9695
write("#T(it, wrappedResponse, null)", setS3ErrorMetadata)
9796
}
9897
}

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/protocols/core/AwsHttpBindingProtocolGenerator.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ abstract class AwsHttpBindingProtocolGenerator : HttpBindingProtocolGenerator()
121121
listOf(
122122
exceptionBaseSymbol,
123123
RuntimeTypes.Http.readAll,
124-
AwsRuntimeTypes.Core.UnknownServiceErrorException,
125124
AwsRuntimeTypes.Http.withPayload,
126125
AwsRuntimeTypes.Http.setAseErrorMetadata,
127126
).forEach(writer::addImport)
@@ -136,7 +135,7 @@ abstract class AwsHttpBindingProtocolGenerator : HttpBindingProtocolGenerator()
136135
}
137136
.dedent()
138137
.withBlock("} catch (ex: Exception) {", "}") {
139-
withBlock("""throw #T("failed to parse response as ${ctx.protocol.name} error", ex).also {""", "}", AwsRuntimeTypes.Core.UnknownServiceErrorException) {
138+
withBlock("""throw #T("Failed to parse response as '${ctx.protocol.name}' error", ex).also {""", "}", exceptionBaseSymbol) {
140139
write("#T(it, wrappedResponse, null)", AwsRuntimeTypes.Http.setAseErrorMetadata)
141140
}
142141
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
package aws.sdk.kotlin.codegen.protocols.core
6+
7+
import software.amazon.smithy.kotlin.codegen.core.KotlinWriter
8+
import software.amazon.smithy.kotlin.codegen.model.expectShape
9+
import software.amazon.smithy.kotlin.codegen.rendering.protocol.HttpBindingResolver
10+
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator
11+
import software.amazon.smithy.kotlin.codegen.test.defaultSettings
12+
import software.amazon.smithy.kotlin.codegen.test.newTestContext
13+
import software.amazon.smithy.kotlin.codegen.test.shouldContainOnlyOnceWithDiff
14+
import software.amazon.smithy.kotlin.codegen.test.toSmithyModel
15+
import software.amazon.smithy.model.Model
16+
import software.amazon.smithy.model.shapes.OperationShape
17+
import software.amazon.smithy.model.shapes.ServiceShape
18+
import software.amazon.smithy.model.shapes.Shape
19+
import software.amazon.smithy.model.shapes.ShapeId
20+
import software.amazon.smithy.model.traits.TimestampFormatTrait
21+
import kotlin.test.Test
22+
23+
class AwsHttpBindingProtocolGeneratorTest {
24+
25+
@Test
26+
fun `it throws base service exception on error parse failure`() {
27+
val model = """
28+
namespace com.test
29+
use aws.protocols#restJson1
30+
31+
@restJson1
32+
service Example {
33+
version: "1.0.0",
34+
operations: [GetFoo]
35+
}
36+
37+
operation GetFoo {
38+
errors: [FooError]
39+
}
40+
41+
@error("server")
42+
structure FooError {
43+
payload: String
44+
}
45+
""".toSmithyModel()
46+
47+
// This is the value that produces the name of the service base exception type
48+
val serviceSdkName = "SdkName"
49+
50+
val testCtx = model.newTestContext(
51+
serviceName = "Example",
52+
settings = model.defaultSettings(sdkId = serviceSdkName)
53+
)
54+
val writer = KotlinWriter("com.test")
55+
val unit = TestableAwsHttpBindingProtocolGenerator()
56+
val op = model.expectShape<OperationShape>("com.test#GetFoo")
57+
58+
unit.renderThrowOperationError(testCtx.generationCtx, op, writer)
59+
60+
val actual = writer.toString()
61+
val expected = """
62+
throw ${serviceSdkName}Exception("Failed to parse response as 'restJson1' error", ex).also {
63+
""".trimIndent()
64+
65+
actual.shouldContainOnlyOnceWithDiff(expected)
66+
}
67+
68+
// A concrete implementation of AwsHttpBindingProtocolGenerator to exercise renderThrowOperationError()
69+
class TestableAwsHttpBindingProtocolGenerator : AwsHttpBindingProtocolGenerator() {
70+
override fun renderDeserializeErrorDetails(
71+
ctx: ProtocolGenerator.GenerationContext,
72+
op: OperationShape,
73+
writer: KotlinWriter
74+
) {
75+
// NOP
76+
}
77+
78+
override val defaultTimestampFormat: TimestampFormatTrait.Format
79+
get() = throw RuntimeException("Unneeded for test")
80+
81+
override fun getProtocolHttpBindingResolver(model: Model, serviceShape: ServiceShape): HttpBindingResolver {
82+
throw RuntimeException("Unneeded for test")
83+
}
84+
85+
override fun renderSerializeOperationBody(
86+
ctx: ProtocolGenerator.GenerationContext,
87+
op: OperationShape,
88+
writer: KotlinWriter
89+
) {
90+
throw RuntimeException("Unneeded for test")
91+
}
92+
93+
override fun renderDeserializeOperationBody(
94+
ctx: ProtocolGenerator.GenerationContext,
95+
op: OperationShape,
96+
writer: KotlinWriter
97+
) {
98+
throw RuntimeException("Unneeded for test")
99+
}
100+
101+
override fun renderSerializeDocumentBody(
102+
ctx: ProtocolGenerator.GenerationContext,
103+
shape: Shape,
104+
writer: KotlinWriter
105+
) {
106+
throw RuntimeException("Unneeded for test")
107+
}
108+
109+
override fun renderDeserializeDocumentBody(
110+
ctx: ProtocolGenerator.GenerationContext,
111+
shape: Shape,
112+
writer: KotlinWriter
113+
) {
114+
throw RuntimeException("Unneeded for test")
115+
}
116+
117+
override fun renderDeserializeException(
118+
ctx: ProtocolGenerator.GenerationContext,
119+
shape: Shape,
120+
writer: KotlinWriter
121+
) {
122+
throw RuntimeException("Unneeded for test")
123+
}
124+
125+
override val protocol: ShapeId
126+
get() = throw RuntimeException("Unneeded for test")
127+
}
128+
}

0 commit comments

Comments
 (0)