{"record":{"id":"8b19e8ff0033a47b","repo":"plandex-ai/plandex","slug":"error-encoding-custom-models-v","errorCode":null,"errorMessage":"Error encoding custom models: %v","messagePattern":"Error encoding custom models: (.+?)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"app/server/handlers/models.go","lineNumber":400,"sourceCode":"\t\treturn\n\t}\n\n\tmodels, err := db.ListCustomModels(auth.OrgId)\n\tif err != nil {\n\t\tlog.Printf(\"Error fetching custom models: %v\\n\", err)\n\t\thttp.Error(w, \"Failed to fetch custom models: \"+err.Error(), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tvar apiList []*shared.CustomModel\n\tfor _, m := range models {\n\t\tapiList = append(apiList, m.ToApi())\n\t}\n\n\terr = json.NewEncoder(w).Encode(apiList)\n\tif err != nil {\n\t\tlog.Printf(\"Error encoding custom models: %v\\n\", err)\n\t\thttp.Error(w, fmt.Sprintf(\"Error encoding custom models: %v\", err), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tlog.Println(\"Successfully fetched custom models\")\n}\n\nfunc GetCustomProviderHandler(w http.ResponseWriter, r *http.Request) {\n\tauth := Authenticate(w, r, true)\n\tif auth == nil {\n\t\treturn\n\t}\n\n\tid := mux.Vars(r)[\"providerId\"]\n\n\tres, err := db.GetCustomProvider(auth.OrgId, id)\n\tif err != nil {\n\t\thttp.Error(w, err.Error(), http.StatusInternalServerError)\n\t\treturn","sourceCodeStart":382,"sourceCodeEnd":418,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/handlers/models.go#L382-L418","documentation":"After building the API list, ListCustomModelsHandler encodes it with json.NewEncoder(w).Encode(apiList). If encoding fails the handler returns 500 'Error encoding custom models: <err>'. Like most encode-into-ResponseWriter failures, the root cause is usually a broken client connection rather than unmarshalable data ([]*shared.CustomModel is JSON-safe).","triggerScenarios":"GET list of custom models where the client disconnects or the connection resets while the JSON array is streamed; or writing to the ResponseWriter fails after headers were committed.","commonSituations":"Large model lists truncated by client timeout; proxy closes connection; user navigates away/cancels the request mid-download.","solutions":["Check caller logs for an aborted/cancelled request — the write failure follows the disconnect.","Increase client/proxy timeouts if large lists are being cut off mid-response.","If it occurs with healthy connections, verify CustomModel.ToApi() has not introduced unmarshalable fields.","Consider marshaling to a buffer first so encode errors are distinguishable from transport errors."],"exampleFix":"// before\nerr = json.NewEncoder(w).Encode(apiList)\nif err != nil {\n    http.Error(w, fmt.Sprintf(\"Error encoding custom models: %v\", err), http.StatusInternalServerError)\n}\n// after\nbuf, err := json.Marshal(apiList)\nif err != nil {\n    http.Error(w, \"encoding failed\", http.StatusInternalServerError)\n    return\n}\nw.Header().Set(\"Content-Type\", \"application/json\")\nw.Write(buf)","handlingStrategy":"try-catch","validationCode":"// Caller: keep the connection alive and honor context cancellation\nctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)\ndefer cancel()\nreq, _ := http.NewRequestWithContext(ctx, \"GET\", listURL, nil)","typeGuard":null,"tryCatchPattern":"if err := json.NewEncoder(w).Encode(apiList); err != nil {\n    log.Printf(\"Error encoding custom models: %v\", err) // likely client disconnect\n    return\n}","preventionTips":["Marshal to a buffer before writing to distinguish encode vs transport failures","Paginate large model lists so responses complete within proxy timeouts","Set adequate client and proxy read timeouts for list endpoints","Keep CustomModel.ToApi() JSON-marshalable"],"tags":["go","http","json-encoding","network"],"backgroundTag":"http-response-write-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}