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