geektutu/7days-golang · error
err.Error()
Error message
err.Error()
What it means
Serialization failure handler in Context.JSON: json.Encoder.Encode failed to marshal the response object (unsupported type, channel/func field, unexported-only struct, cyclic reference), so a plain-text 500 with the encoder error is written instead of JSON. It fires at response-writing time, not request-parsing time.
Source
Thrown at gee-web/day2-context/gee/context.go:59
c.Writer.WriteHeader(code)
}
func (c *Context) SetHeader(key string, value string) {
c.Writer.Header().Set(key, value)
}
func (c *Context) String(code int, format string, values ...interface{}) {
c.SetHeader("Content-Type", "text/plain")
c.Status(code)
c.Writer.Write([]byte(fmt.Sprintf(format, values...)))
}
func (c *Context) JSON(code int, obj interface{}) {
c.SetHeader("Content-Type", "application/json")
c.Status(code)
encoder := json.NewEncoder(c.Writer)
if err := encoder.Encode(obj); err != nil {
http.Error(c.Writer, err.Error(), 500)
}
}
func (c *Context) Data(code int, data []byte) {
c.Status(code)
c.Writer.Write(data)
}
func (c *Context) HTML(code int, html string) {
c.SetHeader("Content-Type", "text/html")
c.Status(code)
c.Writer.Write([]byte(html))
}
View on GitHub (pinned to cf36443821)
Solutions
- Ensure the response object is JSON-serializable: no channels, funcs, or cyclic references; export fields you want encoded.
- Pre-marshal in tests with json.Marshal to catch unsupported types before serving.
- Return a structured JSON error object instead of the default http.Error text.
Defensive patterns
Strategy: fallback
When it happens
Trigger: Thrown at gee-web/day2-context/gee/context.go:59 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/f9b5edb2a3b6d613.
Report an issue: GitHub.