-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a code generated
Unhandled
error
- Loading branch information
Showing
4 changed files
with
64 additions
and
17 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
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
41 changes: 41 additions & 0 deletions
41
...ftware/amazon/smithy/rust/codegen/core/smithy/generators/error/UnhandledErrorGenerator.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,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) { | ||
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. | ||
""", | ||
) | ||
rust("##[derive(Debug)]") | ||
rustBlock("pub struct Unhandled") { | ||
rust("source: Box<dyn #T + Send + Sync + 'static>", RuntimeType.StdError) | ||
} | ||
rustBlock("impl Unhandled") { | ||
rustBlock("pub(crate) fn new(source: Box<dyn #T + Send + Sync + 'static>) -> Self", RuntimeType.StdError) { | ||
rust("Self { source }") | ||
} | ||
} | ||
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\")") | ||
} | ||
} | ||
rustBlock("impl std::error::Error for Unhandled") { | ||
rustBlock("fn source(&self) -> Option<&(dyn std::error::Error + 'static)>") { | ||
rust("Some(self.source.as_ref() as _)") | ||
} | ||
} | ||
} |
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