use tonic::Status;

pub type Result<T> = std::result::Result<T, NarsilError>;

#[derive(Debug, thiserror::Error)]
pub enum NarsilError {
    #[error("invalid request: {0}")]
    InvalidRequest(String),
    #[error("schema error: {0}")]
    Schema(String),
    #[error("backend error: {0}")]
    Backend(String),
    #[error("transport error: {0}")]
    Transport(String),
    #[error("overloaded: {0}")]
    Overloaded(String),
}

impl NarsilError {
    pub fn into_status(self) -> Status {
        match self {
            Self::InvalidRequest(message) | Self::Schema(message) => {
                Status::invalid_argument(message)
            }
            Self::Backend(message) => Status::internal(message),
            Self::Transport(message) => Status::unavailable(message),
            Self::Overloaded(msg) => Status::resource_exhausted(msg),
        }
    }
}
