Skip to main content

rs_tenant/
error.rs

1use crate::ids::{RoleId, TenantId};
2#[cfg(feature = "platform")]
3use crate::platform::PlatformRoleId;
4use thiserror::Error;
5
6/// 授权数据源错误类型。
7pub type SourceError = Box<dyn std::error::Error + Send + Sync>;
8
9/// crate 内统一使用的结果类型。
10pub type Result<T> = std::result::Result<T, Error>;
11
12/// 这个 crate 返回的错误。
13#[derive(Debug, Error)]
14pub enum Error {
15    /// 授权数据源错误包装。
16    #[error("source error: {0}")]
17    Source(#[source] SourceError),
18    /// 标识符输入非法。
19    #[error("invalid id: {0}")]
20    InvalidId(String),
21    /// 权限输入非法。
22    #[error("invalid permission: {0}")]
23    InvalidPermission(String),
24    /// 范围输入非法。
25    #[error("invalid scope: {0}")]
26    InvalidScope(String),
27    /// 检测到角色继承环。
28    #[error("role cycle detected for tenant {tenant} at role {role}")]
29    RoleCycleDetected { tenant: TenantId, role: RoleId },
30    /// 角色继承深度超过限制。
31    #[error(
32        "role inheritance depth exceeded for tenant {tenant} at role {role}; max depth {max_depth}"
33    )]
34    RoleDepthExceeded {
35        tenant: TenantId,
36        role: RoleId,
37        max_depth: usize,
38    },
39    /// 检测到平台角色继承环。
40    #[cfg(feature = "platform")]
41    #[error("platform role cycle detected at role {role}")]
42    PlatformRoleCycleDetected {
43        /// 检测到环的角色。
44        role: PlatformRoleId,
45    },
46    /// 平台角色继承深度超过限制。
47    #[cfg(feature = "platform")]
48    #[error("platform role inheritance depth exceeded at role {role}; max depth {max_depth}")]
49    PlatformRoleDepthExceeded {
50        /// 超过深度限制的角色。
51        role: PlatformRoleId,
52        /// 配置的最大继承深度。
53        max_depth: usize,
54    },
55}
56
57impl From<SourceError> for Error {
58    /// 将授权数据源错误包装为 crate 错误。
59    fn from(error: SourceError) -> Self {
60        Self::Source(error)
61    }
62}