1
- use crate :: proto:: CoreError ;
2
1
use axum:: {
3
2
http:: StatusCode ,
4
3
response:: { IntoResponse , Response } ,
5
4
Json ,
6
5
} ;
7
6
use serde_json:: json;
8
- use tonic:: metadata:: errors:: InvalidMetadataValue ;
9
- use tonic:: { Code , Status } ;
7
+ use tonic:: { metadata:: errors:: InvalidMetadataValue , Code , Status } ;
8
+
9
+ use crate :: proto:: CoreError ;
10
10
11
11
#[ derive( thiserror:: Error , Debug ) ]
12
12
pub enum ApiError {
@@ -24,6 +24,8 @@ pub enum ApiError {
24
24
InvalidResponseType ,
25
25
#[ error( "Permission denied: {0}" ) ]
26
26
PermissionDenied ( String ) ,
27
+ #[ error( "Enterprise not enabled" ) ]
28
+ EnterpriseNotEnabled ,
27
29
}
28
30
29
31
impl IntoResponse for ApiError {
@@ -33,6 +35,10 @@ impl IntoResponse for ApiError {
33
35
Self :: Unauthorized ( msg) => ( StatusCode :: UNAUTHORIZED , msg) ,
34
36
Self :: BadRequest ( msg) => ( StatusCode :: BAD_REQUEST , msg) ,
35
37
Self :: PermissionDenied ( msg) => ( StatusCode :: FORBIDDEN , msg) ,
38
+ Self :: EnterpriseNotEnabled => (
39
+ StatusCode :: PAYMENT_REQUIRED ,
40
+ "Enterprise features are not enabled" . to_string ( ) ,
41
+ ) ,
36
42
_ => (
37
43
StatusCode :: INTERNAL_SERVER_ERROR ,
38
44
"Internal server error" . to_string ( ) ,
@@ -55,6 +61,11 @@ impl From<CoreError> for ApiError {
55
61
Code :: Unauthenticated => ApiError :: Unauthorized ( status. message ( ) . to_string ( ) ) ,
56
62
Code :: InvalidArgument => ApiError :: BadRequest ( status. message ( ) . to_string ( ) ) ,
57
63
Code :: PermissionDenied => ApiError :: PermissionDenied ( status. message ( ) . to_string ( ) ) ,
64
+ Code :: FailedPrecondition => match status. message ( ) . to_lowercase ( ) . as_str ( ) {
65
+ // TODO: find a better way than matching on the error message
66
+ "no valid license" => ApiError :: EnterpriseNotEnabled ,
67
+ _ => ApiError :: Unexpected ( status. to_string ( ) ) ,
68
+ } ,
58
69
_ => ApiError :: Unexpected ( status. to_string ( ) ) ,
59
70
}
60
71
}
0 commit comments