Skip to content
Merged
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
36 changes: 34 additions & 2 deletions transaction-status-client-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,16 @@ impl<'de> DeserializeTrait<'de> for UiTransactionError {
let instruction_error = arr.get(1).ok_or_else(|| {
DeserializeError::invalid_length(1, &"Expected there to be at least 2 elements")
})?;
let err: InstructionError = from_value(instruction_error.clone())
.map_err(|e| DeserializeError::custom(e.to_string()))?;

// Handle SDK version compatibility: if it's a v2-style
// {"BorshIoError": "Unknown"}, convert it to a v3-style
// "BorshIoError"
let err: InstructionError = if instruction_error.get("BorshIoError").is_some() {
from_value(serde_json::json!("BorshIoError"))
} else {
from_value(instruction_error.clone())
}
.map_err(|e| DeserializeError::custom(e.to_string()))?;
return Ok(UiTransactionError(TransactionError::InstructionError(
outer_instruction_index,
err,
Expand Down Expand Up @@ -965,4 +973,28 @@ mod test {
.expect("Failed to deserialize `UiTransactionError");
assert_eq!(actual_transaction_error, expected_transaction_error);
}

#[test]
fn test_deserialize_instruction_error_string_format() {
// Test that we can deserialize new InstructionErrors when serialized as
// a string.
let new_error = TransactionError::InstructionError(0, InstructionError::BorshIoError);
let error_json = to_value(&new_error).unwrap();
let result = from_value::<UiTransactionError>(error_json);
assert!(matches!(
result.unwrap().0,
TransactionError::InstructionError(0, InstructionError::BorshIoError)
));

// This checks compatibility across SDK versions where BorshIoError is
// a unit variant in v3 and newtype variant in v2.
let old_error =
to_value(serde_json::json!({"InstructionError": [0, {"BorshIoError": "Unknown"}]}))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

won't this lend to breakage should we wind up with a typo here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I take your point that we don't get a compile-error from a misspelling here, which we would for InstructionError::BorshIoError. On the flip-side, if the test starts failing because we made a typo, that means the test is doing its job, right?

We'd only get a false positive if we change this string, the one on line 316, but not the one on line 317. While that's possible, I'd argue that it's very unlikely. What do you think?

.unwrap();
let result = from_value::<UiTransactionError>(old_error);
assert!(matches!(
result.unwrap().0,
TransactionError::InstructionError(0, InstructionError::BorshIoError)
));
}
}
Loading