use std::time::Duration;
use thiserror::Error;
#[derive(Error, Debug, Clone)]
pub enum TypesRegistryError {
#[error("Invalid GTS type-schema id: {0}")]
InvalidGtsTypeId(String),
#[error("Invalid GTS instance id: {0}")]
InvalidGtsInstanceId(String),
#[error("GTS type-schema not found: {0}")]
GtsTypeSchemaNotFound(String),
#[error("GTS instance not found: {0}")]
GtsInstanceNotFound(String),
#[error(
"Cannot register {dependent_id}: required type-schema {parent_type_id} is not registered"
)]
ParentTypeSchemaNotRegistered {
parent_type_id: String,
dependent_id: String,
},
#[error("Entity already exists: {0}")]
AlreadyExists(String),
#[error("Invalid query: {0}")]
InvalidQuery(String),
#[error("Validation failed: {0}")]
ValidationFailed(String),
#[error("Service unavailable: {message} (retry after {retry_after:?})")]
ServiceUnavailable {
message: String,
retry_after: Duration,
},
#[error("Internal error: {0}")]
Internal(String),
}
impl TypesRegistryError {
#[must_use]
pub fn invalid_gts_type_id(message: impl Into<String>) -> Self {
Self::InvalidGtsTypeId(message.into())
}
#[must_use]
pub fn invalid_gts_instance_id(message: impl Into<String>) -> Self {
Self::InvalidGtsInstanceId(message.into())
}
#[must_use]
pub fn gts_type_schema_not_found(id_or_uuid: impl Into<String>) -> Self {
Self::GtsTypeSchemaNotFound(id_or_uuid.into())
}
#[must_use]
pub fn gts_instance_not_found(id_or_uuid: impl Into<String>) -> Self {
Self::GtsInstanceNotFound(id_or_uuid.into())
}
#[must_use]
pub fn parent_type_schema_not_registered(
parent_type_id: impl Into<String>,
dependent_id: impl Into<String>,
) -> Self {
Self::ParentTypeSchemaNotRegistered {
parent_type_id: parent_type_id.into(),
dependent_id: dependent_id.into(),
}
}
#[must_use]
pub fn already_exists(gts_id: impl Into<String>) -> Self {
Self::AlreadyExists(gts_id.into())
}
#[must_use]
pub fn invalid_query(message: impl Into<String>) -> Self {
Self::InvalidQuery(message.into())
}
#[must_use]
pub fn validation_failed(message: impl Into<String>) -> Self {
Self::ValidationFailed(message.into())
}
#[must_use]
pub fn service_unavailable(message: impl Into<String>, retry_after: Duration) -> Self {
Self::ServiceUnavailable {
message: message.into(),
retry_after,
}
}
#[must_use]
pub const fn is_service_unavailable(&self) -> bool {
matches!(self, Self::ServiceUnavailable { .. })
}
#[must_use]
pub fn internal(message: impl Into<String>) -> Self {
Self::Internal(message.into())
}
#[must_use]
pub const fn is_invalid_gts_type_id(&self) -> bool {
matches!(self, Self::InvalidGtsTypeId(_))
}
#[must_use]
pub const fn is_invalid_gts_instance_id(&self) -> bool {
matches!(self, Self::InvalidGtsInstanceId(_))
}
#[must_use]
pub const fn is_gts_type_schema_not_found(&self) -> bool {
matches!(self, Self::GtsTypeSchemaNotFound(_))
}
#[must_use]
pub const fn is_gts_instance_not_found(&self) -> bool {
matches!(self, Self::GtsInstanceNotFound(_))
}
#[must_use]
pub const fn is_not_found(&self) -> bool {
matches!(
self,
Self::GtsTypeSchemaNotFound(_) | Self::GtsInstanceNotFound(_)
)
}
#[must_use]
pub const fn is_parent_type_schema_not_registered(&self) -> bool {
matches!(self, Self::ParentTypeSchemaNotRegistered { .. })
}
#[must_use]
pub const fn is_already_exists(&self) -> bool {
matches!(self, Self::AlreadyExists(_))
}
#[must_use]
pub const fn is_invalid_query(&self) -> bool {
matches!(self, Self::InvalidQuery(_))
}
#[must_use]
pub const fn is_validation_failed(&self) -> bool {
matches!(self, Self::ValidationFailed(_))
}
}
#[cfg(test)]
#[path = "error_tests.rs"]
mod tests;