OpenNHP/opennhp · error
could not create form file
Error message
could not create form file: %v
What it means
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.
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
Defensive patterns
Strategy: try-catch
Validate before calling
// no meaningful pre-call validation; CreateFormFile on a bytes.Buffer
// only fails on allocation error. Ensure host memory headroom:
var m runtime.MemStats
runtime.ReadMemStats(&m)
if m.Sys > 3<<30 { // example threshold: 3GB in use
log.Warn("high memory usage before upload")
} Try / catch
url, err := dev.UploadFileToNHPServer(filePath)
if err != nil {
if strings.Contains(err.Error(), "could not create form file") {
return fmt.Errorf("multipart setup failed (likely OOM): %w", err)
}
return err
} Prevention
- Ensure memory headroom in constrained deployments
- Treat this branch as an OOM sentinel in monitoring
- Retry the upload after memory pressure clears
When it happens
Trigger: 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.
Common situations: 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).
Understand the failure class
Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.
Related errors
- could not copy file to server
- could not close writer
- could not get file info
- unknown remote provider
- unknown remote provider
AI-assisted analysis of OpenNHP/opennhp@6e04ca5ff0 (2026-09-07).
Data as JSON: /api/errors/5842c9aeece8bdf3.
Report an issue: GitHub.
Appendix: source
Thrown at endpoints/db/utils.go:227
fileInfo, err := file.Stat()
if err != nil {
return "", fmt.Errorf("could not get file info: %v", err)
}
// create upload progress
progress := &UploadProgress{
TotalSize: fileInfo.Size(),
}
startTime := time.Now()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(filePath))
if err != nil {
return "", fmt.Errorf("could not create form file: %v", err)
}
progressReader := &ProgressReader{
Reader: file,
Progress: progress,
}
_, err = io.Copy(part, progressReader)
if err != nil {
return "", fmt.Errorf("could not copy file to server: %v", err)
}
err = writer.Close()
if err != nil {
return "", fmt.Errorf("could not close writer: %v", err)
}
uploadUrl := httpHost + "storage/upload"View on GitHub (pinned to 6e04ca5ff0)