A community based topic aggregation platform built on atproto
1package comments
2
3import (
4 "Coves/internal/core/comments"
5 "encoding/json"
6 "log"
7 "net/http"
8)
9
10// errorResponse represents a standardized JSON error response
11type errorResponse struct {
12 Error string `json:"error"`
13 Message string `json:"message"`
14}
15
16// writeError writes a JSON error response with the given status code
17func writeError(w http.ResponseWriter, statusCode int, errorType, message string) {
18 w.Header().Set("Content-Type", "application/json")
19 w.WriteHeader(statusCode)
20 if err := json.NewEncoder(w).Encode(errorResponse{
21 Error: errorType,
22 Message: message,
23 }); err != nil {
24 log.Printf("Failed to encode error response: %v", err)
25 }
26}
27
28// handleServiceError maps service-layer errors to HTTP responses
29// This follows the error handling pattern from other handlers (post, community)
30func handleServiceError(w http.ResponseWriter, err error) {
31 switch {
32 case comments.IsNotFound(err):
33 writeError(w, http.StatusNotFound, "NotFound", err.Error())
34
35 case comments.IsValidationError(err):
36 writeError(w, http.StatusBadRequest, "InvalidRequest", err.Error())
37
38 default:
39 // Don't leak internal error details to clients
40 log.Printf("Unexpected error in comments handler: %v", err)
41 writeError(w, http.StatusInternalServerError, "InternalServerError",
42 "An internal error occurred")
43 }
44}