Skip to content

Commit

Permalink
feat: user friendly error on asset parse failure (#4229)
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen authored Nov 10, 2023
1 parent f6a06c1 commit 1de7247
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ use std::{

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[serde(untagged)]
#[serde(
expecting = r#"Expected a valid asset specifier. Assets should be specified as upper-case strings, e.g. `"ETH"`, and can be optionally distinguished by chain, e.g. `{ chain: "Ethereum", asset: "ETH" }."#
)]
pub enum RpcAsset {
ImplicitChain(Asset),
ExplicitChain { chain: ForeignChain, asset: Asset },
Expand Down Expand Up @@ -1293,4 +1296,13 @@ mod test {
assert!(try_into_asset(Asset::Usdc, ForeignChain::Bitcoin).is_err());
assert!(try_into_asset(Asset::Btc, ForeignChain::Ethereum).is_err());
}

#[test]
fn test_failed_parse_error_message() {
let error = serde_json::from_str::<RpcAsset>("\"Eth\"").unwrap_err();
assert_eq!(
error.to_string(),
r#"Expected a valid asset specifier. Assets should be specified as upper-case strings, e.g. `"ETH"`, and can be optionally distinguished by chain, e.g. `{ chain: "Ethereum", asset: "ETH" }."#
);
}
}

0 comments on commit 1de7247

Please sign in to comment.