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