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 json.NewEncoder(w).Encode(XRPCError{
22 Error: error,
23 Message: message,
24 })
25}
26
27// handleServiceError converts service errors to appropriate HTTP responses
28func handleServiceError(w http.ResponseWriter, err error) {
29 switch {
30 case communities.IsNotFound(err):
31 writeError(w, http.StatusNotFound, "NotFound", err.Error())
32 case communities.IsConflict(err):
33 if err == communities.ErrHandleTaken {
34 writeError(w, http.StatusConflict, "NameTaken", "Community handle is already taken")
35 } else {
36 writeError(w, http.StatusConflict, "AlreadyExists", err.Error())
37 }
38 case communities.IsValidationError(err):
39 writeError(w, http.StatusBadRequest, "InvalidRequest", err.Error())
40 case err == communities.ErrUnauthorized:
41 writeError(w, http.StatusForbidden, "Forbidden", "You do not have permission to perform this action")
42 case err == communities.ErrMemberBanned:
43 writeError(w, http.StatusForbidden, "Blocked", "You are blocked from this community")
44 default:
45 // Internal server error - log the actual error for debugging
46 // TODO: Use proper logger instead of log package
47 log.Printf("XRPC handler error: %v", err)
48 writeError(w, http.StatusInternalServerError, "InternalServerError", "An internal error occurred")
49 }
50}