Skip to content

Commit

Permalink
Add a code generated Unhandled error
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisanti committed Oct 13, 2022
1 parent 0344c0f commit 64c0280
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ class CombinedErrorGenerator(
rust(
"""
/// An unexpected error, e.g. invalid JSON returned by the service or an unknown error code
Unhandled(Box<dyn #T + Send + Sync + 'static>),
Unhandled(#T),
""",
RuntimeType.StdError,
unhandledError(),
)
}
writer.rustBlock("impl #T for ${errorSymbol.name}", RuntimeType.Display) {
Expand Down Expand Up @@ -214,7 +214,7 @@ class CombinedErrorGenerator(
/// Creates the `${errorSymbol.name}::Unhandled` variant from any error type.
pub fn unhandled(err: impl Into<Box<dyn #{std_error} + Send + Sync + 'static>>) -> Self {
Self {
kind: ${errorSymbol.name}Kind::Unhandled(err.into()),
kind: ${errorSymbol.name}Kind::Unhandled(#{Unhandled}::new(err.into())),
meta: Default::default()
}
}
Expand All @@ -223,7 +223,7 @@ class CombinedErrorGenerator(
pub fn generic(err: #{generic_error}) -> Self {
Self {
meta: err.clone(),
kind: ${errorSymbol.name}Kind::Unhandled(err.into()),
kind: ${errorSymbol.name}Kind::Unhandled(#{Unhandled}::new(err.into())),
}
}
Expand All @@ -248,7 +248,9 @@ class CombinedErrorGenerator(
self.meta.code()
}
""",
"generic_error" to genericError, "std_error" to RuntimeType.StdError,
"generic_error" to genericError,
"std_error" to RuntimeType.StdError,
"Unhandled" to unhandledError(),
)
errors.forEach { error ->
val errorVariantSymbol = symbolProvider.toSymbol(error)
Expand All @@ -264,10 +266,7 @@ class CombinedErrorGenerator(
rustBlock("fn source(&self) -> Option<&(dyn #T + 'static)>", RuntimeType.StdError) {
delegateToVariants(errors, errorSymbol) {
writable {
when (it) {
is VariantMatch.Unhandled -> rust("Some(_inner.as_ref())")
is VariantMatch.Modeled -> rust("Some(_inner)")
}
rust("Some(_inner)")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.documentShape
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlockTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
Expand Down Expand Up @@ -91,17 +92,17 @@ class TopLevelErrorGenerator(private val codegenContext: CodegenContext, private
}
}

private fun RustWriter.renderImplFrom(symbol: RuntimeType, errors: List<ShapeId>) {
private fun RustWriter.renderImplFrom(errorSymbol: RuntimeType, errors: List<ShapeId>) {
if (errors.isNotEmpty() || CodegenTarget.CLIENT == codegenContext.target) {
rustBlock(
"impl<R> From<#T<#T, R>> for Error where R: Send + Sync + std::fmt::Debug + 'static",
sdkError,
symbol,
errorSymbol,
) {
rustBlockTemplate(
"fn from(err: #{SdkError}<#{OpError}, R>) -> Self",
"SdkError" to sdkError,
"OpError" to symbol,
"OpError" to errorSymbol,
) {
rustBlock("match err") {
val operationErrors = errors.map { model.expectShape(it) }
Expand All @@ -110,12 +111,16 @@ class TopLevelErrorGenerator(private val codegenContext: CodegenContext, private
val errSymbol = symbolProvider.toSymbol(errorShape)
rust(
"#TKind::${errSymbol.name}(inner) => Error::${errSymbol.name}(inner),",
symbol,
errorSymbol,
)
}
rust("#TKind::Unhandled(inner) => Error::Unhandled(inner),", symbol)
rustTemplate(
"#{errorSymbol}Kind::Unhandled(inner) => Error::Unhandled(#{unhandled}::new(inner.into())),",
"errorSymbol" to errorSymbol,
"unhandled" to unhandledError(),
)
}
rust("_ => Error::Unhandled(err.into()),")
rust("_ => Error::Unhandled(#T::new(err.into())),", unhandledError())
}
}
}
Expand All @@ -136,7 +141,7 @@ class TopLevelErrorGenerator(private val codegenContext: CodegenContext, private
rust("${sym.name}(#T),", sym)
}
rust("/// An unhandled error occurred.")
rust("Unhandled(Box<dyn #T + Send + Sync + 'static>)", RuntimeType.StdError)
rust("Unhandled(#T)", unhandledError())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.rust.codegen.core.smithy.generators.error

import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.rustlang.docs
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType

internal fun unhandledError(): RuntimeType = RuntimeType.forInlineFun("Unhandled", RustModule.Error) { writer ->
writer.docs(
"""
An unexpected error occurred (e.g., invalid JSON returned by the service or an unknown error code)
Call [`Error::source`](std::error::Error::source) for more details about the underlying cause.
""",
)
writer.rust("##[derive(Debug)]")
writer.rustBlock("pub struct Unhandled") {
rust("source: Box<dyn #T + Send + Sync + 'static>", RuntimeType.StdError)
}
writer.rustBlock("impl Unhandled") {
rustBlock("pub(crate) fn new(source: Box<dyn #T + Send + Sync + 'static>) -> Self", RuntimeType.StdError) {
rust("Self { source }")
}
}
writer.rustBlock("impl std::fmt::Display for Unhandled") {
rustBlock("fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error>") {
rust("write!(f, \"unhandled error\")")
}
}
writer.rustBlock("impl std::error::Error for Unhandled") {
rustBlock("fn source(&self) -> Option<&(dyn std::error::Error + 'static)>") {
rust("Some(self.source.as_ref() as _)")
}
}
}

0 comments on commit 64c0280

Please sign in to comment.