A community based topic aggregation platform built on atproto
1package community 2 3import ( 4 "encoding/json" 5 "log" 6 "net/http" 7 8 "Coves/internal/core/communities" 9) 10 11// XRPCError represents an XRPC error response 12type XRPCError struct { 13 Error string `json:"error"` 14 Message string `json:"message"` 15} 16 17// writeError writes an XRPC error response 18func writeError(w http.ResponseWriter, status int, error, message string) { 19 w.Header().Set("Content-Type", "application/json") 20 w.WriteHeader(status) 21 if err := json.NewEncoder(w).Encode(XRPCError{ 22 Error: error, 23 Message: message, 24 }); err != nil { 25 log.Printf("Failed to encode error response: %v", err) 26 } 27} 28 29// handleServiceError converts service errors to appropriate HTTP responses 30func handleServiceError(w http.ResponseWriter, err error) { 31 switch { 32 case communities.IsNotFound(err): 33 writeError(w, http.StatusNotFound, "NotFound", err.Error()) 34 case communities.IsConflict(err): 35 if err == communities.ErrHandleTaken { 36 writeError(w, http.StatusConflict, "NameTaken", "Community handle is already taken") 37 } else { 38 writeError(w, http.StatusConflict, "AlreadyExists", err.Error()) 39 } 40 case communities.IsValidationError(err): 41 writeError(w, http.StatusBadRequest, "InvalidRequest", err.Error()) 42 case err == communities.ErrUnauthorized: 43 writeError(w, http.StatusForbidden, "Forbidden", "You do not have permission to perform this action") 44 case err == communities.ErrMemberBanned: 45 writeError(w, http.StatusForbidden, "Blocked", "You are blocked from this community") 46 default: 47 // Internal server error - log the actual error for debugging 48 // TODO: Use proper logger instead of log package 49 log.Printf("XRPC handler error: %v", err) 50 writeError(w, http.StatusInternalServerError, "InternalServerError", "An internal error occurred") 51 } 52}