1use std::error::Error;
5
6#[cfg(feature = "tonic")]
7mod tonic;
8#[cfg(feature = "tonic")]
9pub use tonic::*;
10
11#[cfg(feature = "sqlx")]
12mod sqlx;
13#[cfg(feature = "sqlx")]
14pub use sqlx::*;
15
16#[cfg(feature = "validator")]
17mod validator;
18#[cfg(feature = "validator")]
19pub use validator::*;
20
21#[derive(PartialEq, Debug, Clone, Copy)]
22pub enum ErrorCodes {
23 Success = 0,
25 Cancelled = 1,
27 Unknown = 2,
29 InvalidArgument = 3,
31 DeadlineExceeded = 4,
33 NotFound = 5,
35 AlreadyExists = 6,
37 PermissionDenied = 7,
39 ResourceExhausted = 8,
41 FailedPrecondition = 9,
43 Aborted = 10,
45 OutOfRange = 11,
47 Unimplemented = 12,
49 Internal = 13,
51 Unavailable = 14,
53 DataLoss = 15,
55 Unauthenticated = 16,
57 VersionMismatch = 17,
59 UnprocessableEntity = 18,
61}
62
63impl ErrorCodes {
64 pub fn name(&self) -> &'static str {
65 match self {
66 ErrorCodes::InvalidArgument => "InvalidArgumentError",
67 ErrorCodes::NotFound => "NotFoundError",
68 ErrorCodes::Internal => "InternalError",
69 ErrorCodes::VersionMismatch => "VersionMismatchError",
70 _ => "ChromaError",
71 }
72 }
73}
74
75#[cfg(feature = "http")]
76impl From<ErrorCodes> for http::StatusCode {
77 fn from(error_code: ErrorCodes) -> Self {
78 match error_code {
79 ErrorCodes::Success => http::StatusCode::OK,
80 ErrorCodes::Cancelled => http::StatusCode::BAD_REQUEST,
81 ErrorCodes::Unknown => http::StatusCode::INTERNAL_SERVER_ERROR,
82 ErrorCodes::InvalidArgument => http::StatusCode::BAD_REQUEST,
83 ErrorCodes::DeadlineExceeded => http::StatusCode::GATEWAY_TIMEOUT,
84 ErrorCodes::NotFound => http::StatusCode::NOT_FOUND,
85 ErrorCodes::AlreadyExists => http::StatusCode::CONFLICT,
86 ErrorCodes::PermissionDenied => http::StatusCode::FORBIDDEN,
87 ErrorCodes::ResourceExhausted => http::StatusCode::TOO_MANY_REQUESTS,
88 ErrorCodes::FailedPrecondition => http::StatusCode::PRECONDITION_FAILED,
89 ErrorCodes::Aborted => http::StatusCode::BAD_REQUEST,
90 ErrorCodes::OutOfRange => http::StatusCode::BAD_REQUEST,
91 ErrorCodes::Unimplemented => http::StatusCode::NOT_IMPLEMENTED,
92 ErrorCodes::Internal => http::StatusCode::INTERNAL_SERVER_ERROR,
93 ErrorCodes::Unavailable => http::StatusCode::SERVICE_UNAVAILABLE,
94 ErrorCodes::DataLoss => http::StatusCode::INTERNAL_SERVER_ERROR,
95 ErrorCodes::Unauthenticated => http::StatusCode::UNAUTHORIZED,
96 ErrorCodes::VersionMismatch => http::StatusCode::INTERNAL_SERVER_ERROR,
97 ErrorCodes::UnprocessableEntity => http::StatusCode::UNPROCESSABLE_ENTITY,
98 }
99 }
100}
101
102#[cfg(feature = "http")]
103impl From<http::StatusCode> for ErrorCodes {
104 fn from(value: http::StatusCode) -> Self {
105 match value {
106 http::StatusCode::OK => ErrorCodes::Success,
107 http::StatusCode::BAD_REQUEST => ErrorCodes::InvalidArgument,
108 http::StatusCode::UNAUTHORIZED => ErrorCodes::Unauthenticated,
109 http::StatusCode::FORBIDDEN => ErrorCodes::PermissionDenied,
110 http::StatusCode::NOT_FOUND => ErrorCodes::NotFound,
111 http::StatusCode::CONFLICT => ErrorCodes::AlreadyExists,
112 http::StatusCode::TOO_MANY_REQUESTS => ErrorCodes::ResourceExhausted,
113 http::StatusCode::INTERNAL_SERVER_ERROR => ErrorCodes::Internal,
114 http::StatusCode::SERVICE_UNAVAILABLE => ErrorCodes::Unavailable,
115 http::StatusCode::NOT_IMPLEMENTED => ErrorCodes::Unimplemented,
116 http::StatusCode::GATEWAY_TIMEOUT => ErrorCodes::DeadlineExceeded,
117 http::StatusCode::PRECONDITION_FAILED => ErrorCodes::FailedPrecondition,
118 http::StatusCode::UNPROCESSABLE_ENTITY => ErrorCodes::UnprocessableEntity,
119 _ => ErrorCodes::Unknown,
120 }
121 }
122}
123
124pub trait ChromaError: Error + Send {
125 fn code(&self) -> ErrorCodes;
126 fn boxed(self) -> Box<dyn ChromaError>
127 where
128 Self: Sized + 'static,
129 {
130 Box::new(self)
131 }
132 fn should_trace_error(&self) -> bool {
133 true
134 }
135}
136
137impl Error for Box<dyn ChromaError> {}
138
139impl ChromaError for Box<dyn ChromaError> {
140 fn code(&self) -> ErrorCodes {
141 self.as_ref().code()
142 }
143}
144
145impl ChromaError for std::io::Error {
146 fn code(&self) -> ErrorCodes {
147 ErrorCodes::Unknown
148 }
149}