A community based topic aggregation platform built on atproto
1package post 2 3import ( 4 "Coves/internal/core/aggregators" 5 "Coves/internal/core/posts" 6 "encoding/json" 7 "log" 8 "net/http" 9) 10 11type errorResponse struct { 12 Error string `json:"error"` 13 Message string `json:"message"` 14} 15 16// writeError writes a JSON error response 17func writeError(w http.ResponseWriter, statusCode int, errorType, message string) { 18 w.Header().Set("Content-Type", "application/json") 19 w.WriteHeader(statusCode) 20 if err := json.NewEncoder(w).Encode(errorResponse{ 21 Error: errorType, 22 Message: message, 23 }); err != nil { 24 log.Printf("Failed to encode error response: %v", err) 25 } 26} 27 28// handleServiceError maps service errors to HTTP responses 29func handleServiceError(w http.ResponseWriter, err error) { 30 switch { 31 case err == posts.ErrCommunityNotFound: 32 writeError(w, http.StatusNotFound, "CommunityNotFound", 33 "Community not found") 34 35 case err == posts.ErrNotAuthorized: 36 writeError(w, http.StatusForbidden, "NotAuthorized", 37 "You are not authorized to post in this community") 38 39 case err == posts.ErrBanned: 40 writeError(w, http.StatusForbidden, "Banned", 41 "You are banned from this community") 42 43 case posts.IsContentRuleViolation(err): 44 writeError(w, http.StatusBadRequest, "ContentRuleViolation", err.Error()) 45 46 case posts.IsValidationError(err): 47 writeError(w, http.StatusBadRequest, "InvalidRequest", err.Error()) 48 49 case posts.IsNotFound(err): 50 writeError(w, http.StatusNotFound, "NotFound", err.Error()) 51 52 // Check aggregator authorization errors 53 case aggregators.IsUnauthorized(err): 54 writeError(w, http.StatusForbidden, "NotAuthorized", 55 "Aggregator not authorized to post in this community") 56 57 // Check both aggregator and post rate limit errors 58 case aggregators.IsRateLimited(err) || err == posts.ErrRateLimitExceeded: 59 writeError(w, http.StatusTooManyRequests, "RateLimitExceeded", 60 "Rate limit exceeded. Please try again later.") 61 62 default: 63 // Don't leak internal error details to clients 64 log.Printf("Unexpected error in post handler: %v", err) 65 writeError(w, http.StatusInternalServerError, "InternalServerError", 66 "An internal error occurred") 67 } 68}