1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::convert::Infallible;
#[derive(Clone, Copy)]
pub enum ServiceErrorCode {
// BadRequest,
Internal,
// MethodNotAllowed,
// NotFound,
// RateLimited,
// Unavailable,
// UnsupportedMediaType,
// NotSupportedYet, //< Used for API that is available in elasticsearch but is not yet available in Quickwit.
}
impl ServiceErrorCode {
// pub fn to_grpc_status_code(self) -> tonic::Code {
// match self {
// // ServiceErrorCode::BadRequest => tonic::Code::InvalidArgument,
// ServiceErrorCode::Internal => tonic::Code::Internal,
// // ServiceErrorCode::MethodNotAllowed => tonic::Code::InvalidArgument,
// // ServiceErrorCode::NotFound => tonic::Code::NotFound,
// // ServiceErrorCode::RateLimited => tonic::Code::ResourceExhausted,
// // ServiceErrorCode::Unavailable => tonic::Code::Unavailable,
// // ServiceErrorCode::UnsupportedMediaType => tonic::Code::InvalidArgument,
// // ServiceErrorCode::NotSupportedYet => tonic::Code::Unimplemented,
// }
// }
pub fn to_http_status_code(self) -> http::StatusCode {
match self {
// ServiceErrorCode::BadRequest => http::StatusCode::BAD_REQUEST,
ServiceErrorCode::Internal => http::StatusCode::INTERNAL_SERVER_ERROR,
// ServiceErrorCode::MethodNotAllowed => http::StatusCode::METHOD_NOT_ALLOWED,
// ServiceErrorCode::NotFound => http::StatusCode::NOT_FOUND,
// ServiceErrorCode::RateLimited => http::StatusCode::TOO_MANY_REQUESTS,
// ServiceErrorCode::Unavailable => http::StatusCode::SERVICE_UNAVAILABLE,
// ServiceErrorCode::UnsupportedMediaType => http::StatusCode::UNSUPPORTED_MEDIA_TYPE,
// ServiceErrorCode::NotSupportedYet => http::StatusCode::NOT_IMPLEMENTED,
}
}
}
pub trait ServiceError: ToString {
// fn grpc_error(&self) -> tonic::Status {
// let grpc_code = self.status_code().to_grpc_status_code();
// let error_msg = self.to_string();
// tonic::Status::new(grpc_code, error_msg)
// }
fn status_code(&self) -> ServiceErrorCode;
}
impl ServiceError for Infallible {
fn status_code(&self) -> ServiceErrorCode {
unreachable!()
}
}