Skip to content

Commit

Permalink
use command error
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Feb 5, 2024
1 parent addfead commit 94ed95a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
28 changes: 16 additions & 12 deletions crates/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ pub enum AbiDecodedErrorType {

impl From<AbiDecodedErrorType> for String {
fn from(value: AbiDecodedErrorType) -> Self {
match value {
AbiDecodedErrorType::Unknown => "native parser panicked with unknown error!".to_owned(),
AbiDecodedErrorType::Known { name, args, .. } => {
format!("native parser panicked with: {}\n{}", name, args.join("\n"))
}
}
value.to_string()
}
}

impl std::fmt::Display for AbiDecodedErrorType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", std::convert::Into::<String>::into(self.clone()))
match self {
AbiDecodedErrorType::Unknown => {
f.write_str("native parser panicked with unknown error!")
}
AbiDecodedErrorType::Known { name, args, .. } => f.write_str(&format!(
"native parser panicked with: {}\n{}",
name,
args.join("\n")
)),
}
}
}

Expand Down Expand Up @@ -166,23 +170,23 @@ impl<'a> From<PoisonError<MutexGuard<'a, HashMap<String, ForkedEvm>>>> for ForkC
}

#[derive(Debug, Error)]
pub enum ForkParseError<'a> {
pub enum ForkParseError {
#[error("ForkCall error: {0}")]
ForkCallFailed(ForkCallError<'a>),
ForkCallFailed(ForkCallError<'static>),
#[error("{0}")]
AbiDecodedError(AbiDecodedErrorType),
#[error("Invalid Front Matter error: {0}")]
InvalidFrontMatter(#[from] AddOrderArgsError),
}

impl<'a> From<AbiDecodedErrorType> for ForkParseError<'a> {
impl From<AbiDecodedErrorType> for ForkParseError {
fn from(value: AbiDecodedErrorType) -> Self {
Self::AbiDecodedError(value)
}
}

impl<'a> From<ForkCallError<'a>> for ForkParseError<'a> {
fn from(value: ForkCallError<'a>) -> Self {
impl From<ForkCallError<'static>> for ForkParseError {
fn from(value: ForkCallError<'static>) -> Self {
Self::ForkCallFailed(value)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ pub async fn fork_call<'a>(
/// checks the front matter validity and parses the given rainlang string
/// with the deployer parsed from the front matter
/// returns abi encoded expression config on Ok variant
pub async fn fork_parse_rainlang<'a>(
pub async fn fork_parse_rainlang(
rainlang_string: &str,
front_matter: &str,
fork_url: &str,
fork_block_number: u64,
) -> Result<Bytes, ForkParseError<'a>> {
) -> Result<Bytes, ForkParseError> {
let deployer = AddOrderArgs::try_parse_frontmatter(front_matter)?.0;

let calldata = iParserCall {}.abi_encode();
Expand Down
14 changes: 8 additions & 6 deletions tauri-app/src-tauri/src/commands/fork.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use super::super::error::CommandError;
use alloy_primitives::bytes::Bytes;
use rain_orderbook_common::fork::fork_parse_rainlang;

#[tauri::command]
pub async fn fork_parse<'a>(
pub async fn fork_parse(
rainlang: &str,
front_matter: &str,
fork_url: &str,
fork_block_number: u64,
) -> Result<Bytes, String> {
fork_parse_rainlang(rainlang, front_matter, fork_url, fork_block_number)
.await
.map(Bytes::from)
.map_err(|e| e.to_string())
) -> Result<Bytes, CommandError> {
Ok(
fork_parse_rainlang(rainlang, front_matter, fork_url, fork_block_number)
.await
.map(Bytes::from)?,
)
}
10 changes: 10 additions & 0 deletions tauri-app/src-tauri/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloy_ethers_typecast::{client::LedgerClientError, transaction::ReadableClientError};
use alloy_primitives::ruint::FromUintError;
use rain_orderbook_common::error::ForkParseError;
use rain_orderbook_subgraph_client::{OrderbookSubgraphClientError, WriteCsvError};
use serde::{ser::Serializer, Serialize};
use thiserror::Error;
Expand All @@ -24,6 +25,9 @@ pub enum CommandError {

#[error(transparent)]
WriteCsvError(#[from] WriteCsvError),

#[error(transparent)]
ForkParseRainlangError(ForkParseError),
}

impl Serialize for CommandError {
Expand All @@ -36,3 +40,9 @@ impl Serialize for CommandError {
}

pub type CommandResult<T> = Result<T, CommandError>;

impl From<ForkParseError> for CommandError {
fn from(value: ForkParseError) -> Self {
Self::ForkParseRainlangError(value)
}
}

0 comments on commit 94ed95a

Please sign in to comment.