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