1use std::fmt::{self, Display};
5
6use encore_sdk_core::api;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum ErrCode {
11 Canceled,
12 Unknown,
13 InvalidArgument,
14 DeadlineExceeded,
15 NotFound,
16 AlreadyExists,
17 PermissionDenied,
18 ResourceExhausted,
19 FailedPrecondition,
20 Aborted,
21 OutOfRange,
22 Unimplemented,
23 Internal,
24 Unavailable,
25 DataLoss,
26 Unauthenticated,
27}
28
29#[derive(Debug, Clone)]
31pub struct Error {
32 pub code: ErrCode,
33 pub message: String,
34}
35
36impl Error {
37 pub fn new(code: ErrCode, message: impl Into<String>) -> Self {
38 Self {
39 code,
40 message: message.into(),
41 }
42 }
43
44 pub fn internal(message: impl Into<String>) -> Self {
45 Self::new(ErrCode::Internal, message)
46 }
47
48 pub fn invalid_argument(message: impl Into<String>) -> Self {
49 Self::new(ErrCode::InvalidArgument, message)
50 }
51
52 pub fn not_found(message: impl Into<String>) -> Self {
53 Self::new(ErrCode::NotFound, message)
54 }
55
56 pub fn unavailable(message: impl Into<String>) -> Self {
57 Self::new(ErrCode::Unavailable, message)
58 }
59
60 pub fn unauthenticated(message: impl Into<String>) -> Self {
61 Self::new(ErrCode::Unauthenticated, message)
62 }
63
64 pub(crate) fn wrap(code: ErrCode, cause: impl Display) -> Self {
67 Self::new(code, cause.to_string())
68 }
69}
70
71impl Display for Error {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 write!(f, "{:?}: {}", self.code, self.message)
74 }
75}
76
77impl std::error::Error for Error {}
78
79impl From<api::ErrCode> for ErrCode {
80 fn from(c: api::ErrCode) -> Self {
81 match c {
82 api::ErrCode::Canceled => ErrCode::Canceled,
83 api::ErrCode::Unknown => ErrCode::Unknown,
84 api::ErrCode::InvalidArgument => ErrCode::InvalidArgument,
85 api::ErrCode::DeadlineExceeded => ErrCode::DeadlineExceeded,
86 api::ErrCode::NotFound => ErrCode::NotFound,
87 api::ErrCode::AlreadyExists => ErrCode::AlreadyExists,
88 api::ErrCode::PermissionDenied => ErrCode::PermissionDenied,
89 api::ErrCode::ResourceExhausted => ErrCode::ResourceExhausted,
90 api::ErrCode::FailedPrecondition => ErrCode::FailedPrecondition,
91 api::ErrCode::Aborted => ErrCode::Aborted,
92 api::ErrCode::OutOfRange => ErrCode::OutOfRange,
93 api::ErrCode::Unimplemented => ErrCode::Unimplemented,
94 api::ErrCode::Internal => ErrCode::Internal,
95 api::ErrCode::Unavailable => ErrCode::Unavailable,
96 api::ErrCode::DataLoss => ErrCode::DataLoss,
97 api::ErrCode::Unauthenticated => ErrCode::Unauthenticated,
98 }
99 }
100}
101
102impl From<ErrCode> for api::ErrCode {
103 fn from(c: ErrCode) -> Self {
104 match c {
105 ErrCode::Canceled => api::ErrCode::Canceled,
106 ErrCode::Unknown => api::ErrCode::Unknown,
107 ErrCode::InvalidArgument => api::ErrCode::InvalidArgument,
108 ErrCode::DeadlineExceeded => api::ErrCode::DeadlineExceeded,
109 ErrCode::NotFound => api::ErrCode::NotFound,
110 ErrCode::AlreadyExists => api::ErrCode::AlreadyExists,
111 ErrCode::PermissionDenied => api::ErrCode::PermissionDenied,
112 ErrCode::ResourceExhausted => api::ErrCode::ResourceExhausted,
113 ErrCode::FailedPrecondition => api::ErrCode::FailedPrecondition,
114 ErrCode::Aborted => api::ErrCode::Aborted,
115 ErrCode::OutOfRange => api::ErrCode::OutOfRange,
116 ErrCode::Unimplemented => api::ErrCode::Unimplemented,
117 ErrCode::Internal => api::ErrCode::Internal,
118 ErrCode::Unavailable => api::ErrCode::Unavailable,
119 ErrCode::DataLoss => api::ErrCode::DataLoss,
120 ErrCode::Unauthenticated => api::ErrCode::Unauthenticated,
121 }
122 }
123}
124
125impl From<api::Error> for Error {
126 fn from(e: api::Error) -> Self {
127 Self {
128 code: e.code.into(),
129 message: e.message,
130 }
131 }
132}