Skip to content

Commit

Permalink
fix: expose error detail when swap simulation fails (#5485)
Browse files Browse the repository at this point in the history
* fix: expose error detail when swap simulation fails

* chore: rename
  • Loading branch information
dandanlen authored Dec 16, 2024
1 parent fac6363 commit 8c4fd61
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 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 @@ -1183,10 +1183,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::RawMessage(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 @@ -1675,11 +1675,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
6 changes: 5 additions & 1 deletion state-chain/runtime/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,16 @@ impl From<SimulatedSwapInformation!["1.0.0"]> for SimulatedSwapInformation {
#[derive(Debug, Decode, Encode, TypeInfo)]
pub enum DispatchErrorWithMessage {
Module(Vec<u8>),
RawMessage(Vec<u8>),
Other(DispatchError),
}
impl<E: Into<DispatchError>> From<E> for DispatchErrorWithMessage {
fn from(error: E) -> Self {
match error.into() {
DispatchError::Module(sp_runtime::ModuleError { message: Some(message), .. }) =>
DispatchErrorWithMessage::Module(message.as_bytes().to_vec()),
DispatchError::Other(message) =>
DispatchErrorWithMessage::RawMessage(message.as_bytes().to_vec()),
error => DispatchErrorWithMessage::Other(error),
}
}
Expand All @@ -213,7 +216,8 @@ impl<E: Into<DispatchError>> From<E> for DispatchErrorWithMessage {
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::RawMessage(message) => write!(
f,
"{}",
str::from_utf8(message).unwrap_or("<Error message is not valid UTF-8>")
Expand Down

0 comments on commit 8c4fd61

Please sign in to comment.