A community based topic aggregation platform built on atproto
1package communities
2
3import (
4 "errors"
5 "fmt"
6)
7
8// Domain errors for communities
9var (
10 // ErrCommunityNotFound is returned when a community doesn't exist
11 ErrCommunityNotFound = errors.New("community not found")
12
13 // ErrCommunityAlreadyExists is returned when trying to create a community with duplicate DID
14 ErrCommunityAlreadyExists = errors.New("community already exists")
15
16 // ErrHandleTaken is returned when a community handle is already in use
17 ErrHandleTaken = errors.New("community handle is already taken")
18
19 // ErrInvalidHandle is returned when a handle doesn't match the required format
20 ErrInvalidHandle = errors.New("invalid community handle format")
21
22 // ErrInvalidVisibility is returned when visibility value is not valid
23 ErrInvalidVisibility = errors.New("invalid visibility value")
24
25 // ErrUnauthorized is returned when a user lacks permission for an action
26 ErrUnauthorized = errors.New("unauthorized")
27
28 // ErrSubscriptionAlreadyExists is returned when user is already subscribed
29 ErrSubscriptionAlreadyExists = errors.New("already subscribed to this community")
30
31 // ErrSubscriptionNotFound is returned when subscription doesn't exist
32 ErrSubscriptionNotFound = errors.New("subscription not found")
33
34 // ErrBlockNotFound is returned when block doesn't exist
35 ErrBlockNotFound = errors.New("block not found")
36
37 // ErrMembershipNotFound is returned when membership doesn't exist
38 ErrMembershipNotFound = errors.New("membership not found")
39
40 // ErrMemberBanned is returned when trying to perform action as banned member
41 ErrMemberBanned = errors.New("user is banned from this community")
42
43 // ErrInvalidInput is returned for general validation failures
44 ErrInvalidInput = errors.New("invalid input")
45)
46
47// ValidationError wraps input validation errors with field details
48type ValidationError struct {
49 Field string
50 Message string
51}
52
53func (e *ValidationError) Error() string {
54 return fmt.Sprintf("%s: %s", e.Field, e.Message)
55}
56
57// NewValidationError creates a new validation error
58func NewValidationError(field, message string) *ValidationError {
59 return &ValidationError{
60 Field: field,
61 Message: message,
62 }
63}
64
65// IsNotFound checks if error is a "not found" error
66func IsNotFound(err error) bool {
67 return errors.Is(err, ErrCommunityNotFound) ||
68 errors.Is(err, ErrSubscriptionNotFound) ||
69 errors.Is(err, ErrBlockNotFound) ||
70 errors.Is(err, ErrMembershipNotFound)
71}
72
73// IsConflict checks if error is a conflict error (duplicate)
74func IsConflict(err error) bool {
75 return errors.Is(err, ErrCommunityAlreadyExists) ||
76 errors.Is(err, ErrHandleTaken) ||
77 errors.Is(err, ErrSubscriptionAlreadyExists)
78}
79
80// IsValidationError checks if error is a validation error
81func IsValidationError(err error) bool {
82 var valErr *ValidationError
83 return errors.As(err, &valErr) || errors.Is(err, ErrInvalidInput)
84}