Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

errors.rs 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. use near_account_id::ParseAccountError;
  2. use near_client::Error;
  3. use wasm_bindgen::prelude::*;
  4. #[wasm_bindgen]
  5. #[derive(Clone, Debug)]
  6. pub struct ApiError {
  7. r#type: ErrorType,
  8. msg: String,
  9. }
  10. #[wasm_bindgen]
  11. impl ApiError {
  12. pub fn err_msg(&self) -> String {
  13. self.msg.clone()
  14. }
  15. pub fn err_type(&self) -> ErrorType {
  16. self.r#type
  17. }
  18. }
  19. impl ApiError {
  20. pub fn new(r#type: ErrorType, msg: String) -> Self {
  21. Self { r#type, msg }
  22. }
  23. }
  24. #[wasm_bindgen]
  25. #[derive(Clone, Copy, Debug)]
  26. pub enum ErrorType {
  27. Type,
  28. Rpc,
  29. EmptyBlock,
  30. ArgsSerialize,
  31. DeserializeTxResp,
  32. TransactionSerialize,
  33. TransactionExec,
  34. TxNotStarted,
  35. NoSigner,
  36. ParseRpcUrl,
  37. BadContractId,
  38. Other,
  39. }
  40. impl From<Error> for ApiError {
  41. fn from(error: Error) -> Self {
  42. match error {
  43. Error::Type(type_err) => Self {
  44. r#type: ErrorType::Type,
  45. msg: type_err.to_string(),
  46. },
  47. Error::Rpc { error, method } => Self {
  48. r#type: ErrorType::Rpc,
  49. msg: format!("Method: [\"{method}\"], Cause: \"{error}\""),
  50. },
  51. Error::EmptyBlock => Self {
  52. r#type: ErrorType::EmptyBlock,
  53. msg: error.to_string(),
  54. },
  55. Error::ArgsSerialize(err) => Self {
  56. r#type: ErrorType::ArgsSerialize,
  57. msg: err.to_string(),
  58. },
  59. Error::DeserializeTxResp(err) => Self {
  60. r#type: ErrorType::DeserializeTxResp,
  61. msg: err.to_string(),
  62. },
  63. Error::TransactionSerialize(err) => Self {
  64. r#type: ErrorType::TransactionSerialize,
  65. msg: err.to_string(),
  66. },
  67. Error::TransactionExec(err) => Self {
  68. r#type: ErrorType::TransactionExec,
  69. msg: format!("{err:?}"),
  70. },
  71. Error::TxNotStarted => Self {
  72. r#type: ErrorType::TxNotStarted,
  73. msg: error.to_string(),
  74. },
  75. Error::NoSigner => Self {
  76. r#type: ErrorType::NoSigner,
  77. msg: error.to_string(),
  78. },
  79. }
  80. }
  81. }
  82. impl From<near_client::types::Error> for ApiError {
  83. fn from(err: near_client::types::Error) -> Self {
  84. Self {
  85. r#type: ErrorType::Type,
  86. msg: err.to_string(),
  87. }
  88. }
  89. }
  90. impl From<ParseAccountError> for ApiError {
  91. fn from(err: ParseAccountError) -> Self {
  92. Self::new(
  93. ErrorType::BadContractId,
  94. format!("AccountId has wrong format. Cause: {err}"),
  95. )
  96. }
  97. }