1use std::fmt::Debug;
2
3use thiserror::Error;
4use tonic::{Code, Status};
5
6#[derive(Error, Debug)]
7pub enum Error {
8 #[error("{0} is not found")]
9 NotFound(String),
10 #[error("{0} already exists")]
11 AlreadyExists(String),
12 #[error("invalid argument: {0}")]
13 InvalidArgument(String),
14 #[error("invalid response")]
15 InvalidResponse,
16 #[error(transparent)]
17 Unknown(Box<dyn std::error::Error + Send + Sync>),
18}
19
20impl Error {
21 pub fn unknown(err: impl std::error::Error + Send + Sync + 'static) -> Self {
22 Self::Unknown(Box::new(err))
23 }
24}
25
26impl From<Status> for Error {
27 fn from(s: Status) -> Self {
28 match s.code() {
29 Code::NotFound => Error::NotFound(s.message().to_owned()),
30 Code::AlreadyExists => Error::AlreadyExists(s.message().to_owned()),
31 Code::InvalidArgument => Error::InvalidArgument(s.message().to_owned()),
32 _ => Error::unknown(s),
33 }
34 }
35}
36
37pub type Result<T> = std::result::Result<T, Error>;