use thiserror::Error;
#[derive(Debug, Clone, Error)]
pub enum DataScopeError {
#[error("missing user context in request extension")]
MissingUserContext,
#[error("dept tree unavailable: {0}")]
DeptTreeUnavailable(String),
#[error("invalid data scope rule: {0}")]
InvalidRule(String),
#[error("unsafe custom condition: {0}")]
UnsafeCustomCondition(String),
#[error("custom generator not found: {0}")]
GeneratorNotFound(String),
}
impl DataScopeError {
pub fn error_code(&self) -> &'static str {
match self {
Self::MissingUserContext => "DATA_SCOPE_NO_USER_CONTEXT",
Self::DeptTreeUnavailable(_) => "DATA_SCOPE_DEPT_TREE_UNAVAILABLE",
Self::InvalidRule(_) => "DATA_SCOPE_INVALID_RULE",
Self::UnsafeCustomCondition(_) => "DATA_SCOPE_UNSAFE_CUSTOM",
Self::GeneratorNotFound(_) => "DATA_SCOPE_GENERATOR_NOT_FOUND",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_codes() {
assert_eq!(
DataScopeError::MissingUserContext.error_code(),
"DATA_SCOPE_NO_USER_CONTEXT"
);
assert_eq!(
DataScopeError::DeptTreeUnavailable("x".into()).error_code(),
"DATA_SCOPE_DEPT_TREE_UNAVAILABLE"
);
assert_eq!(
DataScopeError::InvalidRule("x".into()).error_code(),
"DATA_SCOPE_INVALID_RULE"
);
assert_eq!(
DataScopeError::UnsafeCustomCondition("x".into()).error_code(),
"DATA_SCOPE_UNSAFE_CUSTOM"
);
assert_eq!(
DataScopeError::GeneratorNotFound("x".into()).error_code(),
"DATA_SCOPE_GENERATOR_NOT_FOUND"
);
}
}