orbis_plugin_api/sdk/
error.rs1use std::fmt;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug)]
10pub enum Error {
11 Json(serde_json::Error),
13
14 State(String),
16
17 Database(String),
19
20 Http(String),
22
23 PermissionDenied(String),
25
26 InvalidInput(String),
28
29 NotFound(String),
31
32 Internal(String),
34
35 Validation(String),
37
38 Timeout(String),
40}
41
42impl fmt::Display for Error {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 match self {
45 Self::Json(e) => write!(f, "JSON error: {}", e),
46 Self::State(msg) => write!(f, "State error: {}", msg),
47 Self::Database(msg) => write!(f, "Database error: {}", msg),
48 Self::Http(msg) => write!(f, "HTTP error: {}", msg),
49 Self::PermissionDenied(msg) => write!(f, "Permission denied: {}", msg),
50 Self::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
51 Self::NotFound(msg) => write!(f, "Not found: {}", msg),
52 Self::Internal(msg) => write!(f, "Internal error: {}", msg),
53 Self::Validation(msg) => write!(f, "Validation error: {}", msg),
54 Self::Timeout(msg) => write!(f, "Timeout: {}", msg),
55 }
56 }
57}
58
59impl std::error::Error for Error {
60 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
61 match self {
62 Self::Json(e) => Some(e),
63 _ => None,
64 }
65 }
66}
67
68impl From<serde_json::Error> for Error {
69 fn from(e: serde_json::Error) -> Self {
70 Self::Json(e)
71 }
72}
73
74impl From<std::string::FromUtf8Error> for Error {
75 fn from(e: std::string::FromUtf8Error) -> Self {
76 Self::InvalidInput(format!("Invalid UTF-8: {}", e))
77 }
78}
79
80impl Error {
81 #[inline]
83 pub fn state<S: Into<String>>(msg: S) -> Self {
84 Self::State(msg.into())
85 }
86
87 #[inline]
89 pub fn database<S: Into<String>>(msg: S) -> Self {
90 Self::Database(msg.into())
91 }
92
93 #[inline]
95 pub fn http<S: Into<String>>(msg: S) -> Self {
96 Self::Http(msg.into())
97 }
98
99 #[inline]
101 pub fn permission_denied<S: Into<String>>(msg: S) -> Self {
102 Self::PermissionDenied(msg.into())
103 }
104
105 #[inline]
107 pub fn invalid_input<S: Into<String>>(msg: S) -> Self {
108 Self::InvalidInput(msg.into())
109 }
110
111 #[inline]
113 pub fn not_found<S: Into<String>>(msg: S) -> Self {
114 Self::NotFound(msg.into())
115 }
116
117 #[inline]
119 pub fn internal<S: Into<String>>(msg: S) -> Self {
120 Self::Internal(msg.into())
121 }
122
123 #[inline]
125 pub fn validation<S: Into<String>>(msg: S) -> Self {
126 Self::Validation(msg.into())
127 }
128
129 #[must_use]
131 pub const fn status_code(&self) -> u16 {
132 match self {
133 Self::Json(_) | Self::InvalidInput(_) | Self::Validation(_) => 400,
134 Self::PermissionDenied(_) => 403,
135 Self::NotFound(_) => 404,
136 Self::Timeout(_) => 408,
137 Self::State(_) | Self::Database(_) | Self::Http(_) | Self::Internal(_) => 500,
138 }
139 }
140}