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 // ErrBlockAlreadyExists is returned when user has already blocked the community
38 ErrBlockAlreadyExists = errors.New("community already blocked")
39
40 // ErrMembershipNotFound is returned when membership doesn't exist
41 ErrMembershipNotFound = errors.New("membership not found")
42
43 // ErrMemberBanned is returned when trying to perform action as banned member
44 ErrMemberBanned = errors.New("user is banned from this community")
45
46 // ErrInvalidInput is returned for general validation failures
47 ErrInvalidInput = errors.New("invalid input")
48)
49
50// ValidationError wraps input validation errors with field details
51type ValidationError struct {
52 Field string
53 Message string
54}
55
56func (e *ValidationError) Error() string {
57 return fmt.Sprintf("%s: %s", e.Field, e.Message)
58}
59
60// NewValidationError creates a new validation error
61func NewValidationError(field, message string) *ValidationError {
62 return &ValidationError{
63 Field: field,
64 Message: message,
65 }
66}
67
68// IsNotFound checks if error is a "not found" error
69func IsNotFound(err error) bool {
70 return errors.Is(err, ErrCommunityNotFound) ||
71 errors.Is(err, ErrSubscriptionNotFound) ||
72 errors.Is(err, ErrBlockNotFound) ||
73 errors.Is(err, ErrMembershipNotFound)
74}
75
76// IsConflict checks if error is a conflict error (duplicate)
77func IsConflict(err error) bool {
78 return errors.Is(err, ErrCommunityAlreadyExists) ||
79 errors.Is(err, ErrHandleTaken) ||
80 errors.Is(err, ErrSubscriptionAlreadyExists) ||
81 errors.Is(err, ErrBlockAlreadyExists)
82}
83
84// IsValidationError checks if error is a validation error
85func IsValidationError(err error) bool {
86 var valErr *ValidationError
87 return errors.As(err, &valErr) || errors.Is(err, ErrInvalidInput)
88}