Skip to main content

systemprompt_users/
error.rs

1//! Typed error surface for the users crate.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use systemprompt_database::IdentifierError;
7use systemprompt_identifiers::UserId;
8use systemprompt_models::domain_error;
9
10domain_error! {
11    pub enum UserError {
12        common: [repository, validation],
13
14        #[error("user not found: {0}")]
15        NotFound(UserId),
16
17        #[error("user already exists with email: {0}")]
18        EmailAlreadyExists(String),
19
20        #[error("invalid status: {0}")]
21        InvalidStatus(String),
22
23        #[error("invalid role: {0}")]
24        InvalidRole(String),
25
26        #[error("invalid roles: {0:?}")]
27        InvalidRoles(Vec<String>),
28
29        #[error("purge {kind} {name} is not a safe SQL identifier")]
30        PurgeIdentifier {
31            kind: &'static str,
32            name: String,
33            #[source]
34            source: IdentifierError,
35        },
36
37        #[error("account merge is unavailable: no owner reassignments are configured")]
38        MergeUnavailable,
39
40        #[error("owner reassignment failed in the {domain} domain: {source}")]
41        OwnerReassignment {
42            domain: &'static str,
43            #[source]
44            source: systemprompt_traits::RepositoryError,
45        },
46    }
47}
48
49impl From<sqlx::Error> for UserError {
50    fn from(err: sqlx::Error) -> Self {
51        Self::Repository(systemprompt_traits::RepositoryError::from(err))
52    }
53}
54
55pub type Result<T> = std::result::Result<T, UserError>;
56
57pub type UserResult<T> = Result<T>;