Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ members = [

[workspace.dependencies]
uniffi = { version = "0.28.3" }
thiserror = { version = "2.0.7" }
snafu = { version = "0.8" }
wasm-bindgen = { version = "0.2.99" }
tsify-next = { version = "0.5.4", features = ["js"] }
js-sys = { version = "0.3.77" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub async fn {{ operation.rust_function_name }}(
headers.insert("Accept".to_string(), "application/json".to_string());

let body = {% if has_request_body(operation) %}
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
{% else %}
None
{% endif %};
Expand All @@ -137,10 +137,10 @@ pub async fn {{ operation.rust_function_name }}(
{% if request_body_type == "Vec<u8>" %}
Some(p_{{ get_request_body_name(operation) }})
{% else %}
Some(rmp_serde::to_vec_named(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
Some(rmp_serde::to_vec_named(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
{% endif %}
{% else %}
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
{% endif %}
{% else %}
None
Expand All @@ -163,13 +163,13 @@ pub async fn {{ operation.rust_function_name }}(
{% if request_body_type == "Vec<u8>" %}
Some(p_{{ get_request_body_name(operation) }})
{% else %}
Some(rmp_serde::to_vec_named(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
Some(rmp_serde::to_vec_named(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
{% endif %}
} else {
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
}
{% else %}
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde(e.to_string()))?)
Some(serde_json::to_vec(&p_{{ get_request_body_name(operation) }}).map_err(|e| Error::Serde { message: e.to_string() })?)
{% endif %}
{% else %}
None
Expand All @@ -185,7 +185,7 @@ pub async fn {{ operation.rust_function_name }}(
Some(headers),
)
.await
.map_err(Error::Http)?;
.map_err(|e| Error::Http { source: e })?;

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

match ContentType::from(content_type) {
ContentType::Json => serde_json::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string())),
ContentType::Json => serde_json::from_slice(&response.body).map_err(|e| Error::Serde { message: e.to_string() }),
{% if operation.supports_msgpack %}
ContentType::MsgPack => rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string())),
ContentType::MsgPack => rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde { message: e.to_string() }),
{% else %}
ContentType::MsgPack => Err(Error::Serde("MsgPack not supported".to_string())),
ContentType::MsgPack => Err(Error::Serde { message: "MsgPack not supported".to_string() }),
{% endif %}
ContentType::Text => {
let text = String::from_utf8(response.body).map_err(|e| Error::Serde(e.to_string()))?;
Err(Error::Serde(format!("Unexpected text response: {}", text)))
let text = String::from_utf8(response.body).map_err(|e| Error::Serde { message: e.to_string() })?;
Err(Error::Serde { message: format!("Unexpected text response: {}", text) })
},
ContentType::Unsupported(ct) => Err(Error::Serde(format!("Unsupported content type: {}", ct))),
ContentType::Unsupported(ct) => Err(Error::Serde { message: format!("Unsupported content type: {}", ct) }),
}
{% else %}
let _ = response;
Expand Down
28 changes: 15 additions & 13 deletions api/oas_generator/rust_oas_generator/templates/apis/mod.rs.j2
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,37 @@ pub mod parameter_enums;
pub mod {{ operation.rust_function_name }};
{% endfor %}

use snafu::Snafu;

/// Unified error type that can represent any API error from any endpoint
#[derive(Debug, thiserror::Error)]
#[derive(Debug, Snafu)]
pub enum {{ client_type }}ApiError {
{% for operation in operations %}
#[error("{{ operation.rust_function_name | title }} error: {0:?}")]
{{ operation.rust_function_name | pascal_case }}({{ operation.rust_function_name }}::{{ operation.rust_error_enum }}),
#[snafu(display("{{ operation.rust_function_name | title }} error: {error:?}"))]
{{ operation.rust_function_name | pascal_case }} { error: {{ operation.rust_function_name }}::{{ operation.rust_error_enum }} },
{% endfor %}
#[error("Unknown API error: {0}")]
Unknown(String),
#[snafu(display("Unknown API error: {message}"))]
Unknown { message: String },
}

{% for operation in operations %}
impl From<{{ operation.rust_function_name }}::{{ operation.rust_error_enum }}> for {{ client_type }}ApiError {
fn from(err: {{ operation.rust_function_name }}::{{ operation.rust_error_enum }}) -> Self {
{{ client_type }}ApiError::{{ operation.rust_function_name | pascal_case }}(err)
{{ client_type }}ApiError::{{ operation.rust_function_name | pascal_case }} { error: err }
}
}

{% endfor %}

/// The main error type for all {{ client_type | lower }} client operations
#[derive(Debug, thiserror::Error)]
#[derive(Debug, Snafu)]
pub enum Error {
#[error("HTTP error: {0}")]
Http(#[from] algokit_http_client::HttpError),
#[error("Serialization error: {0}")]
Serde(String),
#[error("API error: {0}")]
Api(#[from] {{ client_type }}ApiError),
#[snafu(display("HTTP error: {source}"))]
Http { source: algokit_http_client::HttpError },
#[snafu(display("Serialization error: {message}"))]
Serde { message: String },
#[snafu(display("API error: {source}"))]
Api { source: {{ client_type }}ApiError },
}

#[derive(Debug, PartialEq, Eq)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ rmp-serde = "^1.1"
{% endif %}

# Error handling
thiserror = "^1.0"
snafu = { workspace = true }

# Utilities
base64 = "^0.22"
Expand All @@ -43,4 +43,3 @@ uuid = { version = "^1.0", features = ["v4"] }
[dev-dependencies]
tokio = { version = "1.0", features = ["full"] }
tokio-test = "^0.4"

4 changes: 2 additions & 2 deletions crates/algod_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ algokit_transact = { path = "../algokit_transact" }
rmp-serde = "^1.1"

# Error handling
thiserror = "^1.0"
snafu = { workspace = true }

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

[dev-dependencies]
tokio = { version = "1.0", features = ["full"] }
tokio-test = "^0.4"
tokio-test = "^0.4"
26 changes: 16 additions & 10 deletions crates/algod_client/src/apis/abort_catchup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub async fn abort_catchup(
Some(headers),
)
.await
.map_err(Error::Http)?;
.map_err(|e| Error::Http { source: e })?;

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

match ContentType::from(content_type) {
ContentType::Json => {
serde_json::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string()))
}
ContentType::MsgPack => Err(Error::Serde("MsgPack not supported".to_string())),
ContentType::Json => serde_json::from_slice(&response.body).map_err(|e| Error::Serde {
message: e.to_string(),
}),
ContentType::MsgPack => Err(Error::Serde {
message: "MsgPack not supported".to_string(),
}),
ContentType::Text => {
let text = String::from_utf8(response.body).map_err(|e| Error::Serde(e.to_string()))?;
Err(Error::Serde(format!("Unexpected text response: {}", text)))
}
ContentType::Unsupported(ct) => {
Err(Error::Serde(format!("Unsupported content type: {}", ct)))
let text = String::from_utf8(response.body).map_err(|e| Error::Serde {
message: e.to_string(),
})?;
Err(Error::Serde {
message: format!("Unexpected text response: {}", text),
})
}
ContentType::Unsupported(ct) => Err(Error::Serde {
message: format!("Unsupported content type: {}", ct),
}),
}
}
28 changes: 16 additions & 12 deletions crates/algod_client/src/apis/account_application_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub async fn account_application_information(
Some(headers),
)
.await
.map_err(Error::Http)?;
.map_err(|e| Error::Http { source: e })?;

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

match ContentType::from(content_type) {
ContentType::Json => {
serde_json::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string()))
}
ContentType::MsgPack => {
rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string()))
}
ContentType::Json => serde_json::from_slice(&response.body).map_err(|e| Error::Serde {
message: e.to_string(),
}),
ContentType::MsgPack => rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde {
message: e.to_string(),
}),
ContentType::Text => {
let text = String::from_utf8(response.body).map_err(|e| Error::Serde(e.to_string()))?;
Err(Error::Serde(format!("Unexpected text response: {}", text)))
}
ContentType::Unsupported(ct) => {
Err(Error::Serde(format!("Unsupported content type: {}", ct)))
let text = String::from_utf8(response.body).map_err(|e| Error::Serde {
message: e.to_string(),
})?;
Err(Error::Serde {
message: format!("Unexpected text response: {}", text),
})
}
ContentType::Unsupported(ct) => Err(Error::Serde {
message: format!("Unsupported content type: {}", ct),
}),
}
}
28 changes: 16 additions & 12 deletions crates/algod_client/src/apis/account_asset_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub async fn account_asset_information(
Some(headers),
)
.await
.map_err(Error::Http)?;
.map_err(|e| Error::Http { source: e })?;

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

match ContentType::from(content_type) {
ContentType::Json => {
serde_json::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string()))
}
ContentType::MsgPack => {
rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde(e.to_string()))
}
ContentType::Json => serde_json::from_slice(&response.body).map_err(|e| Error::Serde {
message: e.to_string(),
}),
ContentType::MsgPack => rmp_serde::from_slice(&response.body).map_err(|e| Error::Serde {
message: e.to_string(),
}),
ContentType::Text => {
let text = String::from_utf8(response.body).map_err(|e| Error::Serde(e.to_string()))?;
Err(Error::Serde(format!("Unexpected text response: {}", text)))
}
ContentType::Unsupported(ct) => {
Err(Error::Serde(format!("Unsupported content type: {}", ct)))
let text = String::from_utf8(response.body).map_err(|e| Error::Serde {
message: e.to_string(),
})?;
Err(Error::Serde {
message: format!("Unexpected text response: {}", text),
})
}
ContentType::Unsupported(ct) => Err(Error::Serde {
message: format!("Unsupported content type: {}", ct),
}),
}
}
Loading
Loading