Skip to content

Commit

Permalink
Add From impl to convert operation errors to top-level errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisanti committed Nov 15, 2022
1 parent d059112 commit d459f04
Showing 1 changed file with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class TopLevelErrorGenerator(private val codegenContext: CodegenContext, private

private fun RustWriter.renderImplFrom(errorSymbol: RuntimeType, errors: List<ShapeId>) {
if (errors.isNotEmpty() || CodegenTarget.CLIENT == codegenContext.target) {
val operationErrors = errors.map { model.expectShape(it) }
rustBlock(
"impl<R> From<#T<#T, R>> for Error where R: Send + Sync + std::fmt::Debug + 'static",
sdkError,
Expand All @@ -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(),
)
}
}
}
Expand Down

0 comments on commit d459f04

Please sign in to comment.