123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- 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<Error> 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<near_client::types::Error> for ApiError {
- fn from(err: near_client::types::Error) -> Self {
- Self {
- r#type: ErrorType::Type,
- msg: err.to_string(),
- }
- }
- }
-
- impl From<ParseAccountError> for ApiError {
- fn from(err: ParseAccountError) -> Self {
- Self::new(
- ErrorType::BadContractId,
- format!("AccountId has wrong format. Cause: {err}"),
- )
- }
- }
|