Skip to main content

fastauth_plugin_organization/
error.rs

1//! The error codes the organization endpoints answer with.
2
3use fastauth_core::plugin_prelude::*;
4
5/// Why an organization request failed. Codes, messages and statuses match Better Auth's
6/// organization plugin, so its client's `error.code` checks keep working.
7#[derive(AuthError, Clone, Debug, PartialEq, Eq)]
8pub enum OrganizationError {
9    #[auth_error(
10        status = 403,
11        message = "You are not allowed to create a new organization"
12    )]
13    YouAreNotAllowedToCreateANewOrganization,
14    #[auth_error(
15        status = 403,
16        message = "You have reached the maximum number of organizations"
17    )]
18    YouHaveReachedTheMaximumNumberOfOrganizations,
19    #[auth_error(status = 400, message = "Organization already exists")]
20    OrganizationAlreadyExists,
21    #[auth_error(status = 400, message = "Organization slug already taken")]
22    OrganizationSlugAlreadyTaken,
23    #[auth_error(status = 400, message = "Organization not found")]
24    OrganizationNotFound,
25    #[auth_error(status = 400, message = "User is not a member of the organization")]
26    UserIsNotAMemberOfTheOrganization,
27    #[auth_error(
28        status = 403,
29        message = "You are not allowed to update this organization"
30    )]
31    YouAreNotAllowedToUpdateThisOrganization,
32    #[auth_error(
33        status = 403,
34        message = "You are not allowed to delete this organization"
35    )]
36    YouAreNotAllowedToDeleteThisOrganization,
37    #[auth_error(status = 400, message = "No active organization")]
38    NoActiveOrganization,
39    #[auth_error(
40        status = 400,
41        message = "User is already a member of this organization"
42    )]
43    UserIsAlreadyAMemberOfThisOrganization,
44    #[auth_error(status = 400, message = "Member not found")]
45    MemberNotFound,
46    #[auth_error(status = 400, message = "Role not found: {roles}")]
47    RoleNotFound { roles: String },
48    #[auth_error(
49        status = 400,
50        message = "You cannot leave the organization as the only owner"
51    )]
52    YouCannotLeaveTheOrganizationAsTheOnlyOwner,
53    #[auth_error(
54        status = 400,
55        message = "You cannot leave the organization without an owner"
56    )]
57    YouCannotLeaveTheOrganizationWithoutAnOwner,
58    #[auth_error(status = 401, message = "You are not allowed to delete this member")]
59    YouAreNotAllowedToDeleteThisMember,
60    #[auth_error(
61        status = 403,
62        message = "You are not allowed to invite users to this organization"
63    )]
64    YouAreNotAllowedToInviteUsersToThisOrganization,
65    #[auth_error(status = 400, message = "User is already invited to this organization")]
66    UserIsAlreadyInvitedToThisOrganization,
67    #[auth_error(status = 400, message = "Invitation not found")]
68    InvitationNotFound,
69    #[auth_error(status = 403, message = "You are not the recipient of the invitation")]
70    YouAreNotTheRecipientOfTheInvitation,
71    #[auth_error(
72        status = 403,
73        message = "Email verification required before accepting or rejecting invitation"
74    )]
75    EmailVerificationRequiredBeforeAcceptingOrRejectingInvitation,
76    #[auth_error(
77        status = 403,
78        message = "Email verification required to view or list invitations for the session email"
79    )]
80    EmailVerificationRequiredForInvitation,
81    #[auth_error(
82        status = 403,
83        message = "You are not allowed to cancel this invitation"
84    )]
85    YouAreNotAllowedToCancelThisInvitation,
86    #[auth_error(
87        status = 403,
88        message = "You are not allowed to list invitations for this organization"
89    )]
90    YouAreNotAllowedToListInvitations,
91    #[auth_error(
92        status = 400,
93        message = "Inviter is no longer a member of the organization"
94    )]
95    InviterIsNoLongerAMemberOfTheOrganization,
96    #[auth_error(
97        status = 403,
98        message = "You are not allowed to invite a user with this role"
99    )]
100    YouAreNotAllowedToInviteUserWithThisRole,
101    #[auth_error(status = 403, message = "You are not allowed to update this member")]
102    YouAreNotAllowedToUpdateThisMember,
103    #[auth_error(status = 403, message = "You are not a member of this organization")]
104    YouAreNotAMemberOfThisOrganization,
105    /// Not a Better Auth code: the extractors' and `has_permission` callers' denial.
106    #[auth_error(status = 403, message = "Missing permission {permission}")]
107    PermissionDenied { permission: String },
108}
109
110impl OrganizationError {
111    /// The same error with another HTTP status, for the few places where Better Auth answers
112    /// one code with different statuses.
113    pub(crate) fn with_status(self, status: StatusCode) -> AuthError {
114        let err = AuthError::from(self);
115        AuthError::with_message(status, err.code().to_owned(), err.message().to_owned())
116    }
117}