Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expose error detail when swap simulation fails #5485

Merged
merged 2 commits into from
Dec 16, 2024
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
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 @@ -1181,10 +1181,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 @@ -196,13 +196,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 @@ -212,7 +215,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
Loading