Skip to content

Commit 6aef53a

Browse files
authored
Revamp errors in aws-smithy-checksums (#1850)
1 parent 154dffd commit 6aef53a

File tree

3 files changed

+71
-46
lines changed

3 files changed

+71
-46
lines changed

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/error/UnhandledErrorGenerator.kt

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ package software.amazon.smithy.rust.codegen.core.smithy.generators.error
77

88
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
99
import software.amazon.smithy.rust.codegen.core.rustlang.docs
10-
import software.amazon.smithy.rust.codegen.core.rustlang.rust
11-
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
10+
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
1211
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
1312

1413
internal fun unhandledError(): RuntimeType = RuntimeType.forInlineFun("Unhandled", RustModule.Error) {
@@ -19,23 +18,28 @@ internal fun unhandledError(): RuntimeType = RuntimeType.forInlineFun("Unhandled
1918
Call [`Error::source`](std::error::Error::source) for more details about the underlying cause.
2019
""",
2120
)
22-
rust("##[derive(Debug)]")
23-
rustBlock("pub struct Unhandled") {
24-
rust("source: Box<dyn #T + Send + Sync + 'static>", RuntimeType.StdError)
25-
}
26-
rustBlock("impl Unhandled") {
27-
rustBlock("pub(crate) fn new(source: Box<dyn #T + Send + Sync + 'static>) -> Self", RuntimeType.StdError) {
28-
rust("Self { source }")
21+
rustTemplate(
22+
"""
23+
##[derive(Debug)]
24+
pub struct Unhandled {
25+
source: Box<dyn #{StdError} + Send + Sync + 'static>,
26+
}
27+
impl Unhandled {
28+
pub(crate) fn new(source: Box<dyn #{StdError} + Send + Sync + 'static>) -> Self {
29+
Self { source }
30+
}
2931
}
30-
}
31-
rustBlock("impl std::fmt::Display for Unhandled") {
32-
rustBlock("fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error>") {
33-
rust("write!(f, \"unhandled error\")")
32+
impl std::fmt::Display for Unhandled {
33+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
34+
write!(f, "unhandled error")
35+
}
3436
}
35-
}
36-
rustBlock("impl std::error::Error for Unhandled") {
37-
rustBlock("fn source(&self) -> Option<&(dyn std::error::Error + 'static)>") {
38-
rust("Some(self.source.as_ref() as _)")
37+
impl #{StdError} for Unhandled {
38+
fn source(&self) -> Option<&(dyn #{StdError} + 'static)> {
39+
Some(self.source.as_ref() as _)
40+
}
3941
}
40-
}
42+
""",
43+
"StdError" to RuntimeType.StdError,
44+
)
4145
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
use std::error::Error;
7+
use std::fmt;
8+
9+
/// A checksum algorithm was unknown
10+
#[derive(Debug)]
11+
pub struct UnknownChecksumAlgorithmError {
12+
checksum_algorithm: String,
13+
}
14+
15+
impl UnknownChecksumAlgorithmError {
16+
pub(crate) fn new(checksum_algorithm: impl Into<String>) -> Self {
17+
Self {
18+
checksum_algorithm: checksum_algorithm.into(),
19+
}
20+
}
21+
22+
/// The checksum algorithm that is unknown
23+
pub fn checksum_algorithm(&self) -> &str {
24+
&self.checksum_algorithm
25+
}
26+
}
27+
28+
impl fmt::Display for UnknownChecksumAlgorithmError {
29+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30+
write!(
31+
f,
32+
r#"unknown checksum algorithm "{}", please pass a known algorithm name ("crc32", "crc32c", "sha1", "sha256", "md5")"#,
33+
self.checksum_algorithm
34+
)
35+
}
36+
}
37+
38+
impl Error for UnknownChecksumAlgorithmError {}

rust-runtime/aws-smithy-checksums/src/lib.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
//! Checksum calculation and verification callbacks.
77
8+
use crate::error::UnknownChecksumAlgorithmError;
89
use bytes::Bytes;
910
use std::str::FromStr;
1011

1112
pub mod body;
13+
pub mod error;
1214
pub mod http;
1315

1416
// Valid checksum algorithm names
@@ -29,7 +31,7 @@ pub enum ChecksumAlgorithm {
2931
}
3032

3133
impl FromStr for ChecksumAlgorithm {
32-
type Err = Error;
34+
type Err = UnknownChecksumAlgorithmError;
3335

3436
/// Create a new `ChecksumAlgorithm` from an algorithm name. Valid algorithm names are:
3537
/// - "crc32"
@@ -51,9 +53,7 @@ impl FromStr for ChecksumAlgorithm {
5153
} else if checksum_algorithm.eq_ignore_ascii_case(MD5_NAME) {
5254
Ok(Self::Md5)
5355
} else {
54-
Err(Error::UnknownChecksumAlgorithm(
55-
checksum_algorithm.to_owned(),
56-
))
56+
Err(UnknownChecksumAlgorithmError::new(checksum_algorithm))
5757
}
5858
}
5959
}
@@ -82,27 +82,6 @@ impl ChecksumAlgorithm {
8282
}
8383
}
8484

85-
#[derive(Debug, PartialEq)]
86-
pub enum Error {
87-
UnknownChecksumAlgorithm(String),
88-
}
89-
90-
impl std::fmt::Display for Error {
91-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92-
match self {
93-
Self::UnknownChecksumAlgorithm(algorithm) => {
94-
write!(
95-
f,
96-
r#"unknown checksum algorithm "{}", please pass a known algorithm name ("crc32", "crc32c", "sha1", "sha256", "md5")"#,
97-
algorithm
98-
)
99-
}
100-
}
101-
}
102-
}
103-
104-
impl std::error::Error for Error {}
105-
10685
/// Types implementing this trait can calculate checksums.
10786
///
10887
/// Checksum algorithms are used to validate the integrity of data. Structs that implement this trait
@@ -397,10 +376,14 @@ mod tests {
397376
}
398377

399378
#[test]
400-
#[should_panic = "called `Result::unwrap()` on an `Err` value: UnknownChecksumAlgorithm(\"some invalid checksum algorithm\")"]
401379
fn test_checksum_algorithm_returns_error_for_unknown() {
402-
"some invalid checksum algorithm"
380+
let error = "some invalid checksum algorithm"
403381
.parse::<ChecksumAlgorithm>()
404-
.unwrap();
382+
.err()
383+
.expect("it should error");
384+
assert_eq!(
385+
"some invalid checksum algorithm",
386+
error.checksum_algorithm()
387+
);
405388
}
406389
}

0 commit comments

Comments
 (0)