Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions instruction/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ frozen-abi = [
"serde",
"std",
]
serde = ["dep:serde", "dep:serde_derive", "solana-pubkey/serde"]
serde = ["dep:serde", "dep:serde_derive", "dep:serde_json", "solana-pubkey/serde"]
std = []
syscalls = ["std"]

Expand All @@ -34,6 +34,7 @@ borsh = { workspace = true, optional = true }
num-traits = { workspace = true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
solana-frozen-abi = { workspace = true, optional = true }
solana-frozen-abi-macro = { workspace = true, optional = true }
solana-pubkey = { workspace = true, default-features = false }
Expand All @@ -47,7 +48,7 @@ wasm-bindgen = { workspace = true }
solana-define-syscall = { workspace = true }

[dev-dependencies]
solana-instruction = { path = ".", features = ["borsh"] }
solana-instruction = { path = ".", features = ["borsh", "serde"] }

[lints]
workspace = true
69 changes: 69 additions & 0 deletions instruction/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub const INCORRECT_AUTHORITY: u64 = to_builtin!(26);
feature = "serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "serde", serde(remote = "InstructionError"))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum InstructionError {
/// Deprecated! Use CustomError instead!
Expand Down Expand Up @@ -242,6 +243,35 @@ pub enum InstructionError {
// conversions must also be added
}

// This is a variant on a hack proposed to get around deserializing a unit
// variant or a newtype, since there's no way for an enum variant deserializer
// to work with both a newtype and nothing:
// https://github.com/serde-rs/serde/issues/1174#issuecomment-372411280
#[cfg(all(feature = "std", feature = "serde"))]
impl<'de> serde::de::Deserialize<'de> for InstructionError {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let s = serde_json::Value::deserialize(deserializer)?;
if s.as_str().is_some_and(|v| v == "BorshIoError") {
Ok(Self::BorshIoError(String::new()))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had seen that default value is Unknown, should it be from default? but probably empty is valid 😕

https://github.com/anza-xyz/solana-sdk/blob/transaction-error%40v2.2.1/instruction/src/error.rs#L410

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, but decided to go with the simplest option, since I don't even think RPC returns a value for these errors when coming from blocks.

} else {
Self::deserialize(s).map_err(serde::de::Error::custom)
}
}
}

#[cfg(all(feature = "std", feature = "serde"))]
impl serde::ser::Serialize for InstructionError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
Self::serialize(self, serializer)
}
}

#[cfg(feature = "std")]
impl std::error::Error for InstructionError {}

Expand Down Expand Up @@ -462,3 +492,42 @@ impl From<LamportsError> for InstructionError {
}
}
}

#[cfg(test)]
#[cfg(feature = "serde")]
mod tests {
use {super::InstructionError, std::string::ToString};

#[test]
fn deserialize() {
serde_json::from_str::<InstructionError>(r#""InvalidError2""#).unwrap_err();
serde_json::from_str::<InstructionError>(r#"{"InvalidError2": null}"#).unwrap_err();
serde_json::from_str::<InstructionError>(r#"{}"#).unwrap_err();
serde_json::from_str::<InstructionError>(r#""Custom""#).unwrap_err();

assert_eq!(
InstructionError::BorshIoError("".to_string()),
serde_json::from_str::<InstructionError>(r#""BorshIoError""#).unwrap()
);
assert_eq!(
InstructionError::BorshIoError("42".to_string()),
serde_json::from_str::<InstructionError>(r#"{"BorshIoError": "42"}"#).unwrap()
);
assert_eq!(
InstructionError::InvalidError,
serde_json::from_str::<InstructionError>(r#"{"InvalidError": null}"#).unwrap()
);
}

#[test]
fn serialize() {
assert_eq!(
serde_json::to_string(&InstructionError::BorshIoError("42".to_string())).unwrap(),
r#"{"BorshIoError":"42"}"#
);
assert_eq!(
serde_json::to_string(&InstructionError::InvalidError).unwrap(),
r#""InvalidError""#
);
}
}