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