Skip to content

Commit

Permalink
fix: expose error detail when swap simulation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen committed Dec 11, 2024
1 parent 2752fc6 commit 8314d81
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
5 changes: 3 additions & 2 deletions state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,10 +1124,11 @@ impl From<CfApiError> for ErrorObjectOwned {
other => internal_error(other),
},
CfApiError::DispatchError(dispatch_error) => match dispatch_error {
DispatchErrorWithMessage::Module(message) => match std::str::from_utf8(&message) {
DispatchErrorWithMessage::Module(message) |
DispatchErrorWithMessage::AdHoc(message) => match std::str::from_utf8(&message) {
Ok(message) => call_error(std::format!("DispatchError: {message}")),
Err(error) =>
internal_error(format!("Unable to decode DispatchError: {error}")),
internal_error(format!("Unable to decode Module Error Message: {error}")),
},
DispatchErrorWithMessage::Other(error) =>
internal_error(format!("Unable to decode DispatchError: {error:?}")),
Expand Down
4 changes: 2 additions & 2 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1635,11 +1635,11 @@ impl_runtime_apis! {
],
)
],
).map_err(|e| DispatchErrorWithMessage::Other(match e {
).map_err(|e| match e {
BatchExecutionError::SwapLegFailed { .. } => DispatchError::Other("Swap leg failed."),
BatchExecutionError::PriceViolation { .. } => DispatchError::Other("Price Violation: Some swaps failed due to Price Impact Limitations."),
BatchExecutionError::DispatchError { error } => error,
}))?;
})?;

let (
network_fee,
Expand Down
12 changes: 8 additions & 4 deletions state-chain/runtime/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,26 @@ pub struct SimulatedSwapInformationV2 {
#[derive(Debug, Decode, Encode, TypeInfo)]
pub enum DispatchErrorWithMessage {
Module(Vec<u8>),
AdHoc(Vec<u8>),
Other(DispatchError),
}
impl From<DispatchError> for DispatchErrorWithMessage {
fn from(value: DispatchError) -> Self {
match value {
fn from(error: DispatchError) -> Self {
match error {
DispatchError::Module(sp_runtime::ModuleError { message: Some(message), .. }) =>
DispatchErrorWithMessage::Module(message.as_bytes().to_vec()),
value => DispatchErrorWithMessage::Other(value),
DispatchError::Other(message) =>
DispatchErrorWithMessage::AdHoc(message.as_bytes().to_vec()),
error => DispatchErrorWithMessage::Other(error),
}
}
}
#[cfg(feature = "std")]
impl core::fmt::Display for DispatchErrorWithMessage {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
DispatchErrorWithMessage::Module(message) => write!(
DispatchErrorWithMessage::Module(message) |
DispatchErrorWithMessage::AdHoc(message) => write!(
f,
"{}",
str::from_utf8(message).unwrap_or("<Error message is not valid UTF-8>")
Expand Down

0 comments on commit 8314d81

Please sign in to comment.