A community based topic aggregation platform built on atproto
1package timeline
2
3import (
4 "encoding/json"
5 "errors"
6 "log"
7 "net/http"
8
9 "Coves/internal/core/timeline"
10)
11
12// XRPCError represents an XRPC error response
13type XRPCError struct {
14 Error string `json:"error"`
15 Message string `json:"message"`
16}
17
18// writeError writes a JSON error response
19func writeError(w http.ResponseWriter, status int, errorType, message string) {
20 w.Header().Set("Content-Type", "application/json")
21 w.WriteHeader(status)
22
23 resp := XRPCError{
24 Error: errorType,
25 Message: message,
26 }
27
28 if err := json.NewEncoder(w).Encode(resp); err != nil {
29 log.Printf("ERROR: Failed to encode error response: %v", err)
30 }
31}
32
33// handleServiceError maps service errors to HTTP responses
34func handleServiceError(w http.ResponseWriter, err error) {
35 switch {
36 case timeline.IsValidationError(err):
37 writeError(w, http.StatusBadRequest, "InvalidRequest", err.Error())
38 case errors.Is(err, timeline.ErrInvalidCursor):
39 writeError(w, http.StatusBadRequest, "InvalidCursor", "The provided cursor is invalid")
40 case errors.Is(err, timeline.ErrUnauthorized):
41 writeError(w, http.StatusUnauthorized, "AuthenticationRequired", "User must be authenticated")
42 default:
43 log.Printf("ERROR: Timeline service error: %v", err)
44 writeError(w, http.StatusInternalServerError, "InternalServerError", "An error occurred while fetching timeline")
45 }
46}