actors_runtime/
actor_error.rs1use fvm_shared::error::ExitCode;
2use thiserror::Error;
3
4#[derive(Error, Debug, Clone, PartialEq)]
7#[error("ActorError(exit_code: {exit_code:?}, msg: {msg})")]
8pub struct ActorError {
9 exit_code: ExitCode,
11 msg: String,
13}
14
15impl ActorError {
16 pub fn new(exit_code: ExitCode, msg: String) -> Self {
17 Self { exit_code, msg }
18 }
19
20 pub fn exit_code(&self) -> ExitCode {
22 self.exit_code
23 }
24
25 pub fn is_ok(&self) -> bool {
27 self.exit_code == ExitCode::Ok
28 }
29
30 pub fn msg(&self) -> &str {
32 &self.msg
33 }
34
35 pub fn wrap(mut self, msg: impl AsRef<str>) -> Self {
37 self.msg = format!("{}: {}", msg.as_ref(), self.msg);
38 self
39 }
40}
41
42impl From<fvm_shared::encoding::Error> for ActorError {
44 fn from(e: fvm_shared::encoding::Error) -> Self {
45 Self {
46 exit_code: ExitCode::ErrSerialization,
47 msg: e.to_string(),
48 }
49 }
50}
51
52impl From<fvm_shared::encoding::error::Error> for ActorError {
54 fn from(e: fvm_shared::encoding::error::Error) -> Self {
55 Self {
56 exit_code: ExitCode::ErrSerialization,
57 msg: e.to_string(),
58 }
59 }
60}
61
62#[cfg(target_arch = "wasm32")]
65impl From<fvm_sdk::error::ActorDeleteError> for ActorError {
66 fn from(e: fvm_sdk::error::ActorDeleteError) -> Self {
67 use fvm_sdk::error::ActorDeleteError::*;
68 Self {
69 exit_code: match e {
72 BeneficiaryIsSelf => ExitCode::SysErrIllegalActor,
73 BeneficiaryDoesNotExist => ExitCode::SysErrIllegalArgument,
74 },
75 msg: e.to_string(),
76 }
77 }
78}
79
80#[cfg(target_arch = "wasm32")]
83impl From<fvm_sdk::error::NoStateError> for ActorError {
84 fn from(e: fvm_sdk::error::NoStateError) -> Self {
85 Self {
86 exit_code: ExitCode::SysErrIllegalActor,
89 msg: e.to_string(),
90 }
91 }
92}
93
94impl From<ExitCode> for ActorError {
97 fn from(e: ExitCode) -> Self {
98 ActorError {
99 exit_code: e,
100 msg: "".to_string(),
101 }
102 }
103}
104
105#[macro_export]
107macro_rules! actor_error {
108 ( $code:ident; $msg:expr ) => { $crate::ActorError::new(fvm_shared::error::ExitCode::$code, $msg.to_string()) };
110
111 ( $code:ident; $msg:literal $(, $ex:expr)+ ) => {
113 $crate::ActorError::new(fvm_shared::error::ExitCode::$code, format!($msg, $($ex,)*))
114 };
115
116 ( $code:ident, $msg:expr ) => { $crate::actor_error!($code; $msg) };
118
119 ( $code:ident, $msg:literal $(, $ex:expr)+ ) => {
121 $crate::actor_error!($code; $msg $(, $ex)*)
122 };
123}