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 from gee.Get(key) (typically "<key> not exist" from the configured getter) is returned verbatim as the HTTP response. It fires for any cache retrieval failure on /api.

Source

Thrown at gee-cache/day6-single-flight/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

  1. Inspect the response body to identify the underlying cache error (e.g. "<key> not exist").
  2. Pre-check key existence or map not-found to a 404 on the client side.
  3. 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/day6-single-flight/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/a5a7ca2989f74bd1. Report an issue: GitHub.