plandex-ai/plandex · error
Error encoding custom model: %v
Error message
Error encoding custom model: %v
What it means
GetCustomModelHandler writes the fetched custom model to the HTTP response via json.NewEncoder(w).Encode(res.ToApi()). If encoding fails — the response writer has broken/failed (client disconnected, connection reset) or the value cannot be marshaled — the handler logs 'Error encoding custom model' and returns 500 with the same message. In practice this is almost always a broken HTTP pipe, not a data problem.
Source
Thrown at app/server/handlers/models.go:366
id := mux.Vars(r)["modelId"]
res, err := db.GetCustomModel(auth.OrgId, id)
if err != nil {
log.Printf("Error fetching custom model: %v\n", err)
http.Error(w, "Failed to fetch custom model: "+err.Error(), http.StatusInternalServerError)
return
}
if res == nil {
http.Error(w, "Custom model not found", http.StatusNotFound)
return
}
err = json.NewEncoder(w).Encode(res.ToApi())
if err != nil {
log.Printf("Error encoding custom model: %v\n", err)
http.Error(w, fmt.Sprintf("Error encoding custom model: %v", err), http.StatusInternalServerError)
return
}
log.Println("Successfully fetched custom model")
}
func ListCustomModelsHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Received request for ListCustomModelsHandler")
auth := Authenticate(w, r, true)
if auth == nil {
return
}
if !requireMinClientVersion(w, r, CustomModelsMinClientVersion) {
return
}
View on GitHub (pinned to e2d772072e)
Solutions
- Check client-side logs: the request was likely aborted by the caller — retry with a stable connection.
- Verify any reverse proxy (nginx, ALB) timeout/read-buffer settings are not closing the connection mid-write.
- If it persists with a live client, inspect CustomModel.ToApi() output for unmarshalable values (channels, funcs, invalid UTF-8) added in recent changes.
- Ensure the handler is not writing after the response has already been partially committed elsewhere.
Example fix
// before
err = json.NewEncoder(w).Encode(res.ToApi())
if err != nil {
http.Error(w, fmt.Sprintf("Error encoding custom model: %v", err), http.StatusInternalServerError)
}
// after
buf, err := json.Marshal(res.ToApi())
if err != nil {
http.Error(w, "encoding failed", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(buf) // encode to buffer first; transport errors on w are not client-fixable Defensive patterns
Strategy: try-catch
Validate before calling
// Go: verify the request context is still alive before doing work
if r.Context().Err() != nil { return } // client already gone; skip the DB/encode work Try / catch
if err := json.NewEncoder(w).Encode(res.ToApi()); err != nil {
// after headers are sent you cannot change the status; log and return
log.Printf("encode/transport error: %v", err)
return
} Prevention
- Marshal to a buffer with json.Marshal first, then write once, so data errors differ from transport errors
- Set Content-Type and status before the first write so errors are not half-committed
- Check r.Context().Err() before expensive work to skip aborted requests
- Keep ToApi() outputs JSON-safe: no channels, funcs, or cycles
When it happens
Trigger: Client disconnects or the connection is reset mid-response while json.Encoder writes the GET custom model body; or writing to w returns an error for any transport reason.
Common situations: Client cancels the request or times out before the body is written; reverse proxy/load balancer closes an upstream connection; mobile client drops network during response.
Related errors
- Error encoding custom models: %v
- Error encoding custom provider: %v
- token exchange failed - error reading body: %s
- failed to update context: %v
- failed to download the update: %w
AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05).
Data as JSON: /api/errors/d116f893853f983b.
Report an issue: GitHub.