A community based topic aggregation platform built on atproto
1package community
2
3import (
4 "encoding/json"
5 "net/http"
6
7 "Coves/internal/core/communities"
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 json.NewEncoder(w).Encode(XRPCError{
21 Error: error,
22 Message: message,
23 })
24}
25
26// handleServiceError converts service errors to appropriate HTTP responses
27func handleServiceError(w http.ResponseWriter, err error) {
28 switch {
29 case communities.IsNotFound(err):
30 writeError(w, http.StatusNotFound, "NotFound", err.Error())
31 case communities.IsConflict(err):
32 if err == communities.ErrHandleTaken {
33 writeError(w, http.StatusConflict, "NameTaken", "Community handle is already taken")
34 } else {
35 writeError(w, http.StatusConflict, "AlreadyExists", err.Error())
36 }
37 case communities.IsValidationError(err):
38 writeError(w, http.StatusBadRequest, "InvalidRequest", err.Error())
39 case err == communities.ErrUnauthorized:
40 writeError(w, http.StatusForbidden, "Forbidden", "You do not have permission to perform this action")
41 case err == communities.ErrMemberBanned:
42 writeError(w, http.StatusForbidden, "Blocked", "You are blocked from this community")
43 default:
44 // Internal server error
45 writeError(w, http.StatusInternalServerError, "InternalServerError", "An internal error occurred")
46 }
47}