Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

errors.rs 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. use near_primitives_core::account::id::ParseAccountError;
  2. use near_rpc::Error;
  3. use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi};
  4. use wasm_bindgen::prelude::*;
  5. #[wasm_bindgen]
  6. #[derive(Clone, Debug)]
  7. pub struct ApiError {
  8. r#type: ErrorType,
  9. msg: String,
  10. }
  11. #[wasm_bindgen]
  12. impl ApiError {
  13. pub fn err_msg(&self) -> String {
  14. self.msg.clone()
  15. }
  16. pub fn err_type(&self) -> ErrorType {
  17. self.r#type
  18. }
  19. }
  20. impl ApiError {
  21. pub fn new(r#type: ErrorType, msg: String) -> Self {
  22. Self { r#type, msg }
  23. }
  24. }
  25. impl From<JsValue> for ApiError {
  26. fn from(value: JsValue) -> Self {
  27. // Should be used only for tests.
  28. // No other purpose to use it in code shouldn't appear
  29. unsafe { Self::from_abi(value.into_abi()) }
  30. }
  31. }
  32. #[wasm_bindgen]
  33. #[derive(Clone, Copy, Debug)]
  34. pub enum ErrorType {
  35. NearClient,
  36. Other,
  37. }
  38. impl From<Error> for ApiError {
  39. fn from(error: Error) -> Self {
  40. ApiError {
  41. r#type: ErrorType::NearClient,
  42. msg: error.to_string(),
  43. }
  44. }
  45. }
  46. impl From<ParseAccountError> for ApiError {
  47. fn from(err: ParseAccountError) -> Self {
  48. Self::new(
  49. ErrorType::NearClient,
  50. format!("AccountId has wrong format. Cause: {err}"),
  51. )
  52. }
  53. }