1use std::fmt;
4
5#[derive(Debug)]
23pub struct ProgramError(Box<dyn std::error::Error + Send + Sync>);
24
25impl ProgramError {
26 pub fn new<E>(err: E) -> Self
28 where
29 E: std::error::Error + Send + Sync + 'static,
30 {
31 Self(Box::new(err))
32 }
33
34 pub fn msg(msg: impl Into<String>) -> Self {
36 Self(msg.into().into())
37 }
38}
39
40impl std::error::Error for ProgramError {
41 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42 self.0.source()
43 }
44}
45
46impl fmt::Display for ProgramError {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 write!(f, "rpc program error: {}", self.0)
49 }
50}
51
52impl From<std::io::Error> for ProgramError {
53 fn from(err: std::io::Error) -> Self {
54 Self::new(err)
55 }
56}
57
58impl From<Box<dyn std::error::Error + Send + Sync>> for ProgramError {
59 fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
60 Self(err)
61 }
62}