Files
mcr/internal/oci/catalog.go
Kyle Isom ef39152f4e Add structured error logging to OCI handlers
Every 500 response in the OCI package silently discarded the actual
error, making production debugging impossible. Add slog.Error before
each 500 response with the error and relevant context (repo, digest,
tag, uuid). Add slog.Info for state-mutating successes (manifest push,
blob upload complete, deletions).

Logger is injected into the OCI Handler via constructor, falling back
to slog.Default() if nil.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 12:47:44 -07:00

71 lines
1.6 KiB
Go

package oci
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"git.wntrmute.dev/kyle/mcr/internal/policy"
)
type catalogResponse struct {
Repositories []string `json:"repositories"`
}
func (h *Handler) handleCatalog(w http.ResponseWriter, r *http.Request) {
if !h.checkPolicy(w, r, policy.ActionCatalog, "") {
return
}
n := 0
var err error
if nStr := r.URL.Query().Get("n"); nStr != "" {
n, err = strconv.Atoi(nStr)
if err != nil || n < 0 {
writeOCIError(w, "INVALID_PARAMETER", http.StatusBadRequest, "invalid 'n' parameter")
return
}
}
last := r.URL.Query().Get("last")
if n == 0 {
repos, err := h.db.ListRepositoryNames(last, 10000)
if err != nil {
h.log.Error("catalog: list repository names", "error", err)
writeOCIError(w, "UNKNOWN", http.StatusInternalServerError, "internal error")
return
}
if repos == nil {
repos = []string{}
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(catalogResponse{Repositories: repos})
return
}
repos, err := h.db.ListRepositoryNames(last, n+1)
if err != nil {
h.log.Error("catalog: list repository names (paginated)", "error", err)
writeOCIError(w, "UNKNOWN", http.StatusInternalServerError, "internal error")
return
}
if repos == nil {
repos = []string{}
}
hasMore := len(repos) > n
if hasMore {
repos = repos[:n]
}
if hasMore {
lastRepo := repos[len(repos)-1]
linkURL := fmt.Sprintf("/v2/_catalog?n=%d&last=%s", n, lastRepo)
w.Header().Set("Link", fmt.Sprintf(`<%s>; rel="next"`, linkURL))
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(catalogResponse{Repositories: repos})
}