diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/error/TopLevelErrorGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/error/TopLevelErrorGenerator.kt index cf6e9152769..9edbc111420 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/error/TopLevelErrorGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/error/TopLevelErrorGenerator.kt @@ -95,6 +95,7 @@ class TopLevelErrorGenerator(private val codegenContext: CodegenContext, private private fun RustWriter.renderImplFrom(errorSymbol: RuntimeType, errors: List) { if (errors.isNotEmpty() || CodegenTarget.CLIENT == codegenContext.target) { + val operationErrors = errors.map { model.expectShape(it) } rustBlock( "impl From<#T<#T, R>> for Error where R: Send + Sync + std::fmt::Debug + 'static", sdkError, @@ -106,22 +107,27 @@ class TopLevelErrorGenerator(private val codegenContext: CodegenContext, private "OpError" to errorSymbol, ) { rustBlock("match err") { - val operationErrors = errors.map { model.expectShape(it) } - rustBlock("#T::ServiceError(context) => match context.into_err().kind", sdkError) { - operationErrors.forEach { errorShape -> - val errSymbol = symbolProvider.toSymbol(errorShape) - rust( - "#TKind::${errSymbol.name}(inner) => Error::${errSymbol.name}(inner),", - errorSymbol, - ) - } - rustTemplate( - "#{errorSymbol}Kind::Unhandled(inner) => Error::Unhandled(#{unhandled}::new(inner.into())),", - "errorSymbol" to errorSymbol, - "unhandled" to unhandledError(), + rust("#T::ServiceError(context) => Self::from(context.into_err()),", sdkError) + rust("_ => Error::Unhandled(#T::new(err.into())),", unhandledError()) + } + } + } + + rustBlock("impl From<#T> for Error", errorSymbol) { + rustBlock("fn from(err: #T) -> Self", errorSymbol) { + rustBlock("match err.kind") { + operationErrors.forEach { errorShape -> + val errSymbol = symbolProvider.toSymbol(errorShape) + rust( + "#TKind::${errSymbol.name}(inner) => Error::${errSymbol.name}(inner),", + errorSymbol, ) } - rust("_ => Error::Unhandled(#T::new(err.into())),", unhandledError()) + rustTemplate( + "#{errorSymbol}Kind::Unhandled(inner) => Error::Unhandled(#{unhandled}::new(inner.into())),", + "errorSymbol" to errorSymbol, + "unhandled" to unhandledError(), + ) } } }