Skip to content

Commit

Permalink
Merge pull request #113 from kevinheavey/error-cleanup
Browse files Browse the repository at this point in the history
Error cleanup
  • Loading branch information
kevinheavey authored Oct 2, 2024
2 parents 954ed26 + 8553116 commit 0173fac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Fixed

- Avoid panic in `Keypair.from_base58_string` [(#93)](https://github.com/kevinheavey/solders/pull/93).
- Avoid panic in `Pubkey.create_program_address` [(#111)](https://github.com/kevinheavey/solders/pull/111).
- Avoid panic in `RPCError deserialization` [(#111)](https://github.com/kevinheavey/solders/pull/111).
- Add missing `stack_height` getter [(#103)](https://github.com/kevinheavey/solders/pull/103).

## [0.21.0] - 2024-03-13
Expand Down
12 changes: 8 additions & 4 deletions crates/pubkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ impl Pubkey {
/// Returns:
/// Pubkey: The derived program address.
///
/// Raises:
/// PubkeyError: if the resulting address is invalid.
///
/// Example:
///
/// >>> from solders.pubkey import Pubkey
Expand All @@ -145,10 +148,11 @@ impl Pubkey {
/// 3gF2KMe9KiC6FNVBmfg9i267aMPvK37FewCip4eGBFcT,
/// )
///
pub fn create_program_address(seeds: Vec<&[u8]>, program_id: &Self) -> Self {
PubkeyOriginal::create_program_address(&seeds, &program_id.0)
.expect("Failed to create program address. This is extremely unlikely.")
.into()
pub fn create_program_address(seeds: Vec<&[u8]>, program_id: &Self) -> PyResult<Self> {
handle_py_err(PubkeyOriginal::create_program_address(
&seeds,
&program_id.0,
))
}

#[staticmethod]
Expand Down
12 changes: 10 additions & 2 deletions crates/rpc-responses/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ pub enum RPCError {
MethodNotFoundMessage(MethodNotFoundMessage),
InvalidParamsMessage(InvalidParamsMessage),
InternalErrorMessage(InternalErrorMessage),
Unrecognized(i64),
}

impl RPCError {
Expand All @@ -297,6 +298,7 @@ impl RPCError {
Self::MethodNotFoundMessage(x) => serde_json::to_string(x).unwrap(),
Self::InvalidParamsMessage(x) => serde_json::to_string(x).unwrap(),
Self::InternalErrorMessage(x) => serde_json::to_string(x).unwrap(),
Self::Unrecognized(num) => serde_json::to_string(num).unwrap(),
}
}

Expand Down Expand Up @@ -395,7 +397,8 @@ impl<'de> serde::Deserialize<'de> for RPCError {
Some(-32603) => {
Self::InternalErrorMessage(InternalErrorMessage::deserialize(value).unwrap())
}
type_ => panic!("unsupported type {type_:?}"),
Some(num) => Self::Unrecognized(num),
type_ => return Err(D::Error::custom(format!("unsupported type {type_:?}")))
},
)
}
Expand Down Expand Up @@ -429,6 +432,7 @@ impl Serialize for RPCError {
MethodNotFoundMessage(&'a MethodNotFoundMessage),
InvalidParamsMessage(&'a InvalidParamsMessage),
InternalErrorMessage(&'a InternalErrorMessage),
Unrecognized(i64),
}

#[derive(Serialize)]
Expand Down Expand Up @@ -528,6 +532,10 @@ impl Serialize for RPCError {
t: -32603,
err: RPCError_::InternalErrorMessage(x),
},
RPCError::Unrecognized(num) => RPCErrorWithCode {
t: *num,
err: RPCError_::Unrecognized(*num),
},
};
msg.serialize(serializer)
}
Expand Down Expand Up @@ -1776,7 +1784,7 @@ macro_rules ! pyunion_resp {
match parser {
stringify!($err_variant) => {let parsed = $err_variant::py_from_json(raw)?; let as_enum = Self::RPCError(parsed); Ok(as_enum)},
$(stringify!($variant) => {let parsed = $variant::py_from_json(raw)?; let as_enum = match parsed {Resp::Error {error, ..} => Self::RPCError(error), Resp::Result {result, ..} => Self::$variant(result)};Ok(as_enum)},)+
_ => Err(PyValueError::new_err(format!("Unrecognised parser: {}", parser)))
_ => Err(PyValueError::new_err(format!("Unrecognized parser: {}", parser)))
}
}
}
Expand Down

0 comments on commit 0173fac

Please sign in to comment.