use std::fmt;
use crate::result::{ClassifyError, ErrorCode, 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,
#[error("presence database operation failed")]
Database(#[from] sqlx::Error),
}
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 {
Self::Database(_) => 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",
Self::Database(_) => "presence_database_failed",
})
}
}