use std::fmt;
use crate::result::{ClassifyError, ErrorCode, ErrorDescriptor, ErrorKind};
#[derive(thiserror::Error)]
#[non_exhaustive]
pub enum PresenceError {
#[error("presence scope must contain 1 to 256 UTF-8 bytes without control characters")]
InvalidScope,
#[error("presence user ID must contain 1 to 256 UTF-8 bytes without control characters")]
InvalidUserId,
#[error(
"presence timeout must be a whole number of milliseconds between 1 millisecond and 2147483647 seconds"
)]
InvalidTimeout,
#[error("presence limit must be between 1 and 1000")]
InvalidLimit,
#[cfg(feature = "presence-postgres")]
#[cfg_attr(docsrs, doc(cfg(feature = "presence-postgres")))]
#[error("presence database operation failed")]
Postgres(#[from] crate::db::postgres::PostgresPoolError),
#[cfg(feature = "presence-sqlite")]
#[cfg_attr(docsrs, doc(cfg(feature = "presence-sqlite")))]
#[error("presence database operation failed")]
Sqlite(#[from] crate::db::sqlite::SqlitePoolError),
}
impl fmt::Debug for PresenceError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("PresenceError")
.field("code", &self.error_code())
.finish_non_exhaustive()
}
}
impl ClassifyError for PresenceError {
fn error_kind(&self) -> ErrorKind {
match self {
#[cfg(feature = "presence-postgres")]
Self::Postgres(_) => ErrorKind::Dependency,
#[cfg(feature = "presence-sqlite")]
Self::Sqlite(_) => ErrorKind::Dependency,
Self::InvalidScope
| Self::InvalidUserId
| Self::InvalidTimeout
| Self::InvalidLimit => ErrorKind::Validation,
}
}
fn error_code(&self) -> ErrorCode {
ErrorCode::internal(match self {
Self::InvalidScope => "presence_invalid_scope",
Self::InvalidUserId => "presence_invalid_user_id",
Self::InvalidTimeout => "presence_invalid_timeout",
Self::InvalidLimit => "presence_invalid_limit",
#[cfg(feature = "presence-postgres")]
Self::Postgres(_) => "presence_database_failed",
#[cfg(feature = "presence-sqlite")]
Self::Sqlite(_) => "presence_database_failed",
})
}
fn error_descriptor(&self) -> ErrorDescriptor {
let retryable = match self {
#[cfg(feature = "presence-postgres")]
Self::Postgres(source) => source.error_descriptor().retryable(),
#[cfg(feature = "presence-sqlite")]
Self::Sqlite(source) => source.error_descriptor().retryable(),
Self::InvalidScope
| Self::InvalidUserId
| Self::InvalidTimeout
| Self::InvalidLimit => false,
};
ErrorDescriptor::new(self.error_kind(), self.error_code(), self.to_string())
.with_retryable(retryable)
}
}