geektutu/7days-golang · error

err.Error()

Error message

err.Error()

What it means

The group was found but group.Get(key) returned an error; ServeHTTP forwards err.Error() verbatim with HTTP 500. This is a pass-through of the underlying failure — usually the GetterFunc (cache-miss callback) failed, or a peer fetch/ConsistentHash call errored.

Source

Thrown at gee-cache/day3-http-server/geecache/http.go:56

	// /<basepath>/<groupname>/<key> required
	parts := strings.SplitN(r.URL.Path[len(p.basePath):], "/", 2)
	if len(parts) != 2 {
		http.Error(w, "bad request", http.StatusBadRequest)
		return
	}

	groupName := parts[0]
	key := parts[1]

	group := GetGroup(groupName)
	if group == nil {
		http.Error(w, "no such group: "+groupName, http.StatusNotFound)
		return
	}

	view, err := group.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())
}

View on GitHub (pinned to cf36443821)

Solutions

  1. Check the 500 response body — it contains the real underlying error message from group.Get.
  2. Fix or harden the GetterFunc: return a sensible sentinel for 'not found' and log backend failures.
  3. Verify peer connectivity (gee-cache/day5 peers) and retry the request if a peer was temporarily down.
  4. Add timeouts/retries around external calls inside your GetterFunc so transient failures don't surface as raw 500s.

Example fix

// before: getter returns raw DB error
geecache.GetterFunc(func(key string) ([]byte, error) {
    return db.QueryRow("SELECT v FROM t WHERE k=?", key) // misuse
})
// after: load bytes and return a clean error
geecache.GetterFunc(func(key string) ([]byte, error) {
    var v []byte
    if err := db.QueryRow("SELECT v FROM t WHERE k=?", key).Scan(&v); err != nil {
        return nil, fmt.Errorf("backend load %q: %w", key, err)
    }
    return v, nil
})
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-flight: verify backend the getter depends on is reachable
if err := db.Ping(); err != nil {
    return fmt.Errorf("cache getter backend unavailable: %w", err)
}

Try / catch

// Go: handle 500 from the cache HTTP API with the forwarded error body
resp, err := http.Get(url)
if err != nil {
    return err
}
if resp.StatusCode == http.StatusInternalServerError {
    body, _ := io.ReadAll(resp.Body)
    return fmt.Errorf("geecache get failed: %s", body) // contains err.Error() from server
}

Prevention

When it happens

Trigger: Any group.Get(key) failure: the GetterFunc returns an error (DB down, record missing with an error return), the peer HTTP round-trip fails in remote-node mode, or the underlying byteview fetch errors.

Common situations: Backend database unreachable; GetterFunc logic bug (nil result marshaling); network partition between cache nodes; the peer selected by consistent hashing is down.

Related errors


AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03). Data as JSON: /api/errors/d56fec39de08bcc6. Report an issue: GitHub.