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