-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: generate per/service base exception types (#270)
BREAKING: service client and other names may end up different when generated after this commit
- Loading branch information
Showing
10 changed files
with
273 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...egen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/ExceptionBaseClassGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package software.amazon.smithy.kotlin.codegen | ||
|
||
import software.amazon.smithy.codegen.core.Symbol | ||
import software.amazon.smithy.kotlin.codegen.integration.ProtocolGenerator | ||
|
||
/** | ||
* Renders the base class that all (modeled) exceptions inherit from. | ||
* Protocol generators are allowed to override this but they MUST inherit from the base `ServiceException` | ||
* with the expected constructors. | ||
*/ | ||
object ExceptionBaseClassGenerator { | ||
fun render(ctx: CodegenContext, writer: KotlinWriter) { | ||
val baseException = ctx.protocolGenerator?.exceptionBaseClassSymbol ?: ProtocolGenerator.DefaultServiceExceptionSymbol | ||
writer.addImport(baseException) | ||
val serviceException = baseExceptionSymbol(ctx.settings) | ||
|
||
writer.dokka("Base class for all service related exceptions thrown by the ${ctx.settings.sdkId.clientName()} client") | ||
writer.withBlock( | ||
"open class #T : #T {", "}", | ||
serviceException, | ||
baseException | ||
) { | ||
write("constructor() : super()") | ||
write("constructor(message: String?) : super(message)") | ||
write("constructor(message: String?, cause: Throwable?) : super(message, cause)") | ||
write("constructor(cause: Throwable?) : super(cause)") | ||
} | ||
} | ||
|
||
/** | ||
* Get the (generated) symbol that constitutes the base class exceptions will inherit from | ||
*/ | ||
fun baseExceptionSymbol(settings: KotlinSettings): Symbol = buildSymbol { | ||
val serviceName = settings.sdkId.clientName() | ||
name = "${serviceName}Exception" | ||
namespace = "${settings.pkg.name}.model" | ||
definitionFile = "$name.kt" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.