{"record":{"id":"5842c9aeece8bdf3","repo":"OpenNHP/opennhp","slug":"could-not-create-form-file-v","errorCode":null,"errorMessage":"could not create form file: %v","messagePattern":"could not create form file: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"endpoints/db/utils.go","lineNumber":227,"sourceCode":"\n\tfileInfo, err := file.Stat()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"could not get file info: %v\", err)\n\t}\n\n\t// create upload progress\n\tprogress := &UploadProgress{\n\t\tTotalSize: fileInfo.Size(),\n\t}\n\n\tstartTime := time.Now()\n\n\tbody := &bytes.Buffer{}\n\twriter := multipart.NewWriter(body)\n\n\tpart, err := writer.CreateFormFile(\"file\", filepath.Base(filePath))\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"could not create form file: %v\", err)\n\t}\n\n\tprogressReader := &ProgressReader{\n\t\tReader:   file,\n\t\tProgress: progress,\n\t}\n\n\t_, err = io.Copy(part, progressReader)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"could not copy file to server: %v\", err)\n\t}\n\n\terr = writer.Close()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"could not close writer: %v\", err)\n\t}\n\n\tuploadUrl := httpHost + \"storage/upload\"","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/endpoints/db/utils.go#L209-L245","documentation":"The multipart writer wraps the file as a form part named 'file' using CreateFormFile. The error 'could not create form file: %v' wraps that failure. In Go, CreateFormFile only fails when writing the part header to the backing buffer errors — for a bytes.Buffer backing this is effectively a non-recoverable internal failure and is extremely rare in practice.","triggerScenarios":"Calling writer.CreateFormFile when the underlying bytes.Buffer write fails — essentially only on memory allocation failure (out of memory) since multipart.WithBufferSize/backing writers are fixed here.","commonSituations":"Running nhp-db in a severely memory-constrained environment where even the tiny part-header allocation fails; corrupting the multipart.Writer via misuse (not applicable in normal use).","solutions":["Check host memory availability; an OOM-condition allocation failure is the realistic cause","Retry the upload after freeing memory","Keep the code as-is otherwise — this branch is defensive and rarely hit","If recurring, profile memory usage of the nhp-db process"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// no meaningful pre-call validation; CreateFormFile on a bytes.Buffer\n// only fails on allocation error. Ensure host memory headroom:\nvar m runtime.MemStats\nruntime.ReadMemStats(&m)\nif m.Sys > 3<<30 { // example threshold: 3GB in use\n    log.Warn(\"high memory usage before upload\")\n}","typeGuard":null,"tryCatchPattern":"url, err := dev.UploadFileToNHPServer(filePath)\nif err != nil {\n    if strings.Contains(err.Error(), \"could not create form file\") {\n        return fmt.Errorf(\"multipart setup failed (likely OOM): %w\", err)\n    }\n    return err\n}","preventionTips":["Ensure memory headroom in constrained deployments","Treat this branch as an OOM sentinel in monitoring","Retry the upload after memory pressure clears"],"tags":["go","multipart","memory","file-upload"],"backgroundTag":"file-write-failed","analyzedSha":"6e04ca5ff03222a699c24205cd4bf8fee9af7ffe","analyzedAt":"2026-09-07T15:44:59.941Z","contentChangedAt":"2026-09-07T15:44:59.941Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}