12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- use near_primitives_core::account::id::ParseAccountError;
- use near_rpc::Error;
- use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi};
- 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 }
- }
- }
-
- impl From<JsValue> for ApiError {
- fn from(value: JsValue) -> Self {
- // Should be used only for tests.
- // No other purpose to use it in code shouldn't appear
- unsafe { Self::from_abi(value.into_abi()) }
- }
- }
-
- #[wasm_bindgen]
- #[derive(Clone, Copy, Debug)]
- pub enum ErrorType {
- NearClient,
- Other,
- }
-
- impl From<Error> for ApiError {
- fn from(error: Error) -> Self {
- ApiError {
- r#type: ErrorType::NearClient,
- msg: error.to_string(),
- }
- }
- }
-
- impl From<ParseAccountError> for ApiError {
- fn from(err: ParseAccountError) -> Self {
- Self::new(
- ErrorType::NearClient,
- format!("AccountId has wrong format. Cause: {err}"),
- )
- }
- }
|