Skip to content

Commit 1f1a6c5

Browse files
joe-pCopilot
andauthored
feat: migrate from thiserror to snafu (#234)
* feat(transact): migrate from thiserror to snafu * feat: migrate other crates from thiserror to snafu * feat: migrate apis from thiserror to snafu * chore: cargo fmt * fix: fix remaining error-related errors * chore: fix extra space in error message Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 3388cc7 commit 1f1a6c5

File tree

130 files changed

+2509
-1861
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+2509
-1861
lines changed

Cargo.lock

Lines changed: 28 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ members = [
2020

2121
[workspace.dependencies]
2222
uniffi = { version = "0.28.3" }
23-
thiserror = { version = "2.0.7" }
23+
snafu = { version = "0.8" }
2424
wasm-bindgen = { version = "0.2.99" }
2525
tsify-next = { version = "0.5.4", features = ["js"] }
2626
js-sys = { version = "0.3.77" }

api/oas_generator/rust_oas_generator/templates/apis/endpoint.rs.j2

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub async fn {{ operation.rust_function_name }}(
123123
headers.insert("Accept".to_string(), "application/json".to_string());
124124

125125
let body = {% if has_request_body(operation) %}
126-
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
126+
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
127127
{% else %}
128128
None
129129
{% endif %};
@@ -137,10 +137,10 @@ pub async fn {{ operation.rust_function_name }}(
137137
{% if request_body_type == "Vec<u8>" %}
138138
Some(p_{{ get_request_body_name(operation) }})
139139
{% else %}
140-
Some(rmp_serde::to_vec_named(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
140+
Some(rmp_serde::to_vec_named(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
141141
{% endif %}
142142
{% else %}
143-
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
143+
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
144144
{% endif %}
145145
{% else %}
146146
None
@@ -163,13 +163,13 @@ pub async fn {{ operation.rust_function_name }}(
163163
{% if request_body_type == "Vec<u8>" %}
164164
Some(p_{{ get_request_body_name(operation) }})
165165
{% else %}
166-
Some(rmp_serde::to_vec_named(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
166+
Some(rmp_serde::to_vec_named(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
167167
{% endif %}
168168
} else {
169-
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
169+
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
170170
}
171171
{% else %}
172-
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
172+
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
173173
{% endif %}
174174
{% else %}
175175
None
@@ -185,7 +185,7 @@ pub async fn {{ operation.rust_function_name }}(
185185
Some(headers),
186186
)
187187
.await
188-
.map_err(Error::Http)?;
188+
.map_err(|e| Error::Http { source: e })?;
189189

190190
{% if get_success_response_type(operation) %}
191191
let content_type = response
@@ -195,17 +195,17 @@ pub async fn {{ operation.rust_function_name }}(
195195
.unwrap_or("application/json");
196196

197197
match ContentType::from(content_type) {
198-
ContentType::Json => serde_json::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string())),
198+
ContentType::Json => serde_json::from_slice(&response.body).map_err(|e| Error::Serde { message: e.to_string() }),
199199
{% if operation.supports_msgpack %}
200-
ContentType::MsgPack => rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string())),
200+
ContentType::MsgPack => rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde { message: e.to_string() }),
201201
{% else %}
202-
ContentType::MsgPack => Err(Error::Serde("MsgPack not supported".to_string())),
202+
ContentType::MsgPack => Err(Error::Serde { message: "MsgPack not supported".to_string() }),
203203
{% endif %}
204204
ContentType::Text => {
205-
let text = String::from_utf8(response.body).map_err(|e| Error::Serde(e.to_string()))?;
206-
Err(Error::Serde(format!("Unexpected text response: {}", text)))
205+
let text = String::from_utf8(response.body).map_err(|e| Error::Serde { message: e.to_string() })?;
206+
Err(Error::Serde { message: format!("Unexpected text response: {}", text) })
207207
},
208-
ContentType::Unsupported(ct) => Err(Error::Serde(format!("Unsupported content type: {}", ct))),
208+
ContentType::Unsupported(ct) => Err(Error::Serde { message: format!("Unsupported content type: {}", ct) }),
209209
}
210210
{% else %}
211211
let _ = response;

api/oas_generator/rust_oas_generator/templates/apis/mod.rs.j2

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,37 @@ pub mod parameter_enums;
2323
pub mod {{ operation.rust_function_name }};
2424
{% endfor %}
2525

26+
use snafu::Snafu;
27+
2628
/// Unified error type that can represent any API error from any endpoint
27-
#[derive(Debug, thiserror::Error)]
29+
#[derive(Debug, Snafu)]
2830
pub enum {{ client_type }}ApiError {
2931
{% for operation in operations %}
30-
#[error("{{ operation.rust_function_name | title }} error: {0:?}")]
31-
{{ operation.rust_function_name | pascal_case }}({{ operation.rust_function_name }}::{{ operation.rust_error_enum }}),
32+
#[snafu(display("{{ operation.rust_function_name | title }} error: {error:?}"))]
33+
{{ operation.rust_function_name | pascal_case }} { error: {{ operation.rust_function_name }}::{{ operation.rust_error_enum }} },
3234
{% endfor %}
33-
#[error("Unknown API error: {0}")]
34-
Unknown(String),
35+
#[snafu(display("Unknown API error: {message}"))]
36+
Unknown { message: String },
3537
}
3638

3739
{% for operation in operations %}
3840
impl From<{{ operation.rust_function_name }}::{{ operation.rust_error_enum }}> for {{ client_type }}ApiError {
3941
fn from(err: {{ operation.rust_function_name }}::{{ operation.rust_error_enum }}) -> Self {
40-
{{ client_type }}ApiError::{{ operation.rust_function_name | pascal_case }}(err)
42+
{{ client_type }}ApiError::{{ operation.rust_function_name | pascal_case }} { error: err }
4143
}
4244
}
4345

4446
{% endfor %}
4547

4648
/// The main error type for all {{ client_type | lower }} client operations
47-
#[derive(Debug, thiserror::Error)]
49+
#[derive(Debug, Snafu)]
4850
pub enum Error {
49-
#[error("HTTP error: {0}")]
50-
Http(#[from] algokit_http_client::HttpError),
51-
#[error("Serialization error: {0}")]
52-
Serde(String),
53-
#[error("API error: {0}")]
54-
Api(#[from] {{ client_type }}ApiError),
51+
#[snafu(display("HTTP error: {source}"))]
52+
Http { source: algokit_http_client::HttpError },
53+
#[snafu(display("Serialization error: {message}"))]
54+
Serde { message: String },
55+
#[snafu(display("API error: {source}"))]
56+
Api { source: {{ client_type }}ApiError },
5557
}
5658

5759
#[derive(Debug, PartialEq, Eq)]

api/oas_generator/rust_oas_generator/templates/base/Cargo.toml.j2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ rmp-serde = "^1.1"
3434
{% endif %}
3535

3636
# Error handling
37-
thiserror = "^1.0"
37+
snafu = { workspace = true }
3838

3939
# Utilities
4040
base64 = "^0.22"
@@ -43,4 +43,3 @@ uuid = { version = "^1.0", features = ["v4"] }
4343
[dev-dependencies]
4444
tokio = { version = "1.0", features = ["full"] }
4545
tokio-test = "^0.4"
46-

crates/algod_client/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ algokit_transact = { path = "../algokit_transact" }
3232
rmp-serde = "^1.1"
3333

3434
# Error handling
35-
thiserror = "^1.0"
35+
snafu = { workspace = true }
3636

3737
# Utilities
3838
base64 = "^0.22"
3939
uuid = { version = "^1.0", features = ["v4"] }
4040

4141
[dev-dependencies]
4242
tokio = { version = "1.0", features = ["full"] }
43-
tokio-test = "^0.4"
43+
tokio-test = "^0.4"

crates/algod_client/src/apis/abort_catchup.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub async fn abort_catchup(
6060
Some(headers),
6161
)
6262
.await
63-
.map_err(Error::Http)?;
63+
.map_err(|e| Error::Http { source: e })?;
6464

6565
let content_type = response
6666
.headers
@@ -69,16 +69,22 @@ pub async fn abort_catchup(
6969
.unwrap_or("application/json");
7070

7171
match ContentType::from(content_type) {
72-
ContentType::Json => {
73-
serde_json::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string()))
74-
}
75-
ContentType::MsgPack => Err(Error::Serde("MsgPack not supported".to_string())),
72+
ContentType::Json => serde_json::from_slice(&response.body).map_err(|e| Error::Serde {
73+
message: e.to_string(),
74+
}),
75+
ContentType::MsgPack => Err(Error::Serde {
76+
message: "MsgPack not supported".to_string(),
77+
}),
7678
ContentType::Text => {
77-
let text = String::from_utf8(response.body).map_err(|e| Error::Serde(e.to_string()))?;
78-
Err(Error::Serde(format!("Unexpected text response: {}", text)))
79-
}
80-
ContentType::Unsupported(ct) => {
81-
Err(Error::Serde(format!("Unsupported content type: {}", ct)))
79+
let text = String::from_utf8(response.body).map_err(|e| Error::Serde {
80+
message: e.to_string(),
81+
})?;
82+
Err(Error::Serde {
83+
message: format!("Unexpected text response: {}", text),
84+
})
8285
}
86+
ContentType::Unsupported(ct) => Err(Error::Serde {
87+
message: format!("Unsupported content type: {}", ct),
88+
}),
8389
}
8490
}

crates/algod_client/src/apis/account_application_information.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub async fn account_application_information(
8080
Some(headers),
8181
)
8282
.await
83-
.map_err(Error::Http)?;
83+
.map_err(|e| Error::Http { source: e })?;
8484

8585
let content_type = response
8686
.headers
@@ -89,18 +89,22 @@ pub async fn account_application_information(
8989
.unwrap_or("application/json");
9090

9191
match ContentType::from(content_type) {
92-
ContentType::Json => {
93-
serde_json::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string()))
94-
}
95-
ContentType::MsgPack => {
96-
rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string()))
97-
}
92+
ContentType::Json => serde_json::from_slice(&response.body).map_err(|e| Error::Serde {
93+
message: e.to_string(),
94+
}),
95+
ContentType::MsgPack => rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde {
96+
message: e.to_string(),
97+
}),
9898
ContentType::Text => {
99-
let text = String::from_utf8(response.body).map_err(|e| Error::Serde(e.to_string()))?;
100-
Err(Error::Serde(format!("Unexpected text response: {}", text)))
101-
}
102-
ContentType::Unsupported(ct) => {
103-
Err(Error::Serde(format!("Unsupported content type: {}", ct)))
99+
let text = String::from_utf8(response.body).map_err(|e| Error::Serde {
100+
message: e.to_string(),
101+
})?;
102+
Err(Error::Serde {
103+
message: format!("Unexpected text response: {}", text),
104+
})
104105
}
106+
ContentType::Unsupported(ct) => Err(Error::Serde {
107+
message: format!("Unsupported content type: {}", ct),
108+
}),
105109
}
106110
}

crates/algod_client/src/apis/account_asset_information.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub async fn account_asset_information(
8080
Some(headers),
8181
)
8282
.await
83-
.map_err(Error::Http)?;
83+
.map_err(|e| Error::Http { source: e })?;
8484

8585
let content_type = response
8686
.headers
@@ -89,18 +89,22 @@ pub async fn account_asset_information(
8989
.unwrap_or("application/json");
9090

9191
match ContentType::from(content_type) {
92-
ContentType::Json => {
93-
serde_json::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string()))
94-
}
95-
ContentType::MsgPack => {
96-
rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string()))
97-
}
92+
ContentType::Json => serde_json::from_slice(&response.body).map_err(|e| Error::Serde {
93+
message: e.to_string(),
94+
}),
95+
ContentType::MsgPack => rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde {
96+
message: e.to_string(),
97+
}),
9898
ContentType::Text => {
99-
let text = String::from_utf8(response.body).map_err(|e| Error::Serde(e.to_string()))?;
100-
Err(Error::Serde(format!("Unexpected text response: {}", text)))
101-
}
102-
ContentType::Unsupported(ct) => {
103-
Err(Error::Serde(format!("Unsupported content type: {}", ct)))
99+
let text = String::from_utf8(response.body).map_err(|e| Error::Serde {
100+
message: e.to_string(),
101+
})?;
102+
Err(Error::Serde {
103+
message: format!("Unexpected text response: {}", text),
104+
})
104105
}
106+
ContentType::Unsupported(ct) => Err(Error::Serde {
107+
message: format!("Unsupported content type: {}", ct),
108+
}),
105109
}
106110
}

0 commit comments

Comments
 (0)