use near_account_id::ParseAccountError; use near_client::Error; use wasm_bindgen::prelude::*; #[wasm_bindgen] #[derive(Clone, Debug)] pub struct ApiError { r#type: ErrorType, msg: String, } #[wasm_bindgen] impl ApiError { pub fn err_msg(&self) -> String { self.msg.clone() } pub fn err_type(&self) -> ErrorType { self.r#type } } impl ApiError { pub fn new(r#type: ErrorType, msg: String) -> Self { Self { r#type, msg } } } #[wasm_bindgen] #[derive(Clone, Copy, Debug)] pub enum ErrorType { Type, Rpc, EmptyBlock, ArgsSerialize, DeserializeTxResp, TransactionSerialize, TransactionExec, TxNotStarted, NoSigner, ParseRpcUrl, BadContractId, Other, } impl From for ApiError { fn from(error: Error) -> Self { match error { Error::Type(type_err) => Self { r#type: ErrorType::Type, msg: type_err.to_string(), }, Error::Rpc { error, method } => Self { r#type: ErrorType::Rpc, msg: format!("Method: [\"{method}\"], Cause: \"{error}\""), }, Error::EmptyBlock => Self { r#type: ErrorType::EmptyBlock, msg: error.to_string(), }, Error::ArgsSerialize(err) => Self { r#type: ErrorType::ArgsSerialize, msg: err.to_string(), }, Error::DeserializeTxResp(err) => Self { r#type: ErrorType::DeserializeTxResp, msg: err.to_string(), }, Error::TransactionSerialize(err) => Self { r#type: ErrorType::TransactionSerialize, msg: err.to_string(), }, Error::TransactionExec(err) => Self { r#type: ErrorType::TransactionExec, msg: format!("{err:?}"), }, Error::TxNotStarted => Self { r#type: ErrorType::TxNotStarted, msg: error.to_string(), }, Error::NoSigner => Self { r#type: ErrorType::NoSigner, msg: error.to_string(), }, } } } impl From for ApiError { fn from(err: near_client::types::Error) -> Self { Self { r#type: ErrorType::Type, msg: err.to_string(), } } } impl From for ApiError { fn from(err: ParseAccountError) -> Self { Self::new( ErrorType::BadContractId, format!("AccountId has wrong format. Cause: {err}"), ) } }