-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy patherror.rs
More file actions
59 lines (55 loc) · 1.84 KB
/
Copy patherror.rs
File metadata and controls
59 lines (55 loc) · 1.84 KB
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
57
58
59
use crate::types::api_keys::ApiKeyError;
use alloy_primitives::hex::FromHexError;
use sea_orm::{sqlx::types::uuid, DbErr};
use std::num::ParseIntError;
use thiserror::Error;
use tonic::Code;
#[derive(Error, Debug)]
pub enum ServiceError {
#[error("api key error: {0}")]
ApiKey(#[from] ApiKeyError),
#[error(transparent)]
Convert(#[from] ParseError),
#[error("internal error: {0}")]
Internal(#[from] anyhow::Error),
#[error("external api error: {0}")]
ExternalApi(#[from] api_client_framework::Error),
#[error("db error: {0}")]
Db(#[from] DbErr),
#[error("not found: {0}")]
NotFound(String),
}
#[derive(Error, Debug)]
pub enum ParseError {
#[error("parse error: invalid integer")]
ParseInt(#[from] ParseIntError),
#[error("parse error: invalid hex")]
ParseHex(#[from] FromHexError),
#[error("parse error: invalid uuid")]
ParseUuid(#[from] uuid::Error),
#[error("parse error: invalid slice")]
TryFromSlice(#[from] core::array::TryFromSliceError),
#[error("parse error: invalid json")]
Json(#[from] serde_json::Error),
#[error("parse error: {0}")]
Custom(String),
}
impl From<ServiceError> for tonic::Status {
fn from(err: ServiceError) -> Self {
let code = match &err {
ServiceError::ApiKey(err) => map_api_key_code(err),
ServiceError::Convert(_) => Code::InvalidArgument,
ServiceError::Internal(_) => Code::Internal,
ServiceError::NotFound(_) => Code::NotFound,
ServiceError::Db(_) => Code::Internal,
ServiceError::ExternalApi(_) => Code::Internal,
};
tonic::Status::new(code, err.to_string())
}
}
fn map_api_key_code(err: &ApiKeyError) -> Code {
match err {
ApiKeyError::InvalidToken(_) => Code::PermissionDenied,
ApiKeyError::Db(_) => Code::Internal,
}
}