geektutu/7days-golang · error
err.Error()
Error message
err.Error()
What it means
Generic 500 handler in the API server of the demo main: any error returned by gee.Get(key) (e.g. key not found from the configured getter) is written verbatim as the HTTP response body. It fires for any cache retrieval failure on /api, so the client sees the underlying error text with status 500.
Source
Thrown at gee-cache/day5-multi-nodes/main.go:50
return nil, fmt.Errorf("%s not exist", key)
}))
}
func startCacheServer(addr string, addrs []string, gee *geecache.Group) {
peers := geecache.NewHTTPPool(addr)
peers.Set(addrs...)
gee.RegisterPeers(peers)
log.Println("geecache is running at", addr)
log.Fatal(http.ListenAndServe(addr[7:], peers))
}
func startAPIServer(apiAddr string, gee *geecache.Group) {
http.Handle("/api", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
key := r.URL.Query().Get("key")
view, err := gee.Get(key)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(view.ByteSlice())
}))
log.Println("fontend server is running at", apiAddr)
log.Fatal(http.ListenAndServe(apiAddr[7:], nil))
}
func main() {
var port int
var api bool
flag.IntVar(&port, "port", 8001, "Geecache server port")
flag.BoolVar(&api, "api", false, "Start a api server?")
flag.Parse()
View on GitHub (pinned to cf36443821)
Solutions
- Inspect the response body to identify the underlying cache error (e.g. "<key> not exist").
- Pre-check key existence or treat not-found as a 404 instead of a generic 500 on the client side.
- Add logging in the API handler to record the full error before returning it.
Defensive patterns
Strategy: fallback
When it happens
Trigger: Thrown at gee-cache/day5-multi-nodes/main.go:50 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03).
Data as JSON: /api/errors/2acb2900018ba497.
Report an issue: GitHub.