anomalyco/sst · error
failed to close writer: %w
Error message
failed to close writer: %w
What it means
Returned when multipart.Writer.Close() fails after all parts were written. Close finalizes the closing boundary into the bytes.Buffer and only errors if that final write fails. This is the last step of building the upload body in handleUpdate before the HTTP PUT to the Cloudflare Workers API.
Source
Thrown at pkg/server/resource/cloudflare-worker-script.go:201
})
if err != nil {
return fmt.Errorf("failed to create form part metadata: %w", err)
}
metadata, err := json.Marshal(buildMetadata(input))
if err != nil {
return fmt.Errorf("failed to marshal metadata: %w", err)
}
_, err = metadataPart.Write(metadata)
if err != nil {
return fmt.Errorf("failed to write file content for metadata: %w", err)
}
// Close writer
err = writer.Close()
if err != nil {
return fmt.Errorf("failed to close writer: %w", err)
}
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/accounts/%s/workers/scripts/%s", input.AccountId, input.ScriptName)
req, err := http.NewRequest("PUT", url, &body)
if err != nil {
return err
}
req.Header.Set("Content-Type", "multipart/form-data; boundary="+writer.Boundary())
req.Header.Set("Authorization", "Bearer "+input.ApiToken)
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
View on GitHub (pinned to a0bd20f762)
Solutions
- Verify all part writes completed before Close and Close is called exactly once
- Inspect the wrapped inner error for buffer/allocation causes
- Report upstream if it reproduces on an unmodified checkout
Example fix
null
Defensive patterns
Strategy: try-catch
Validate before calling
null
Try / catch
if err := handleUpdate(input); err != nil {
if strings.Contains(err.Error(), "failed to close writer") {
return fmt.Errorf("multipart body incomplete: %w", err)
}
return err
} Prevention
- Call writer.Close() exactly once, after all parts
- Set the request Content-Type from writer.Boundary() only after successful Close
When it happens
Trigger: writer.Close() returns an error when writing the terminating multipart boundary into the bytes.Buffer.
Common situations: Memory/buffer failures; custom modifications to the body-building sequence.
Related errors
- failed to create form part %s: %w
- failed to write file content for %s: %w
- failed to create form part metadata: %w
- bucket %v upload failed: %w
- failed to create form part %s: %w
AI-assisted analysis of anomalyco/sst@a0bd20f762 (2026-08-30).
Data as JSON: /api/errors/faef937fc1b5ff47.
Report an issue: GitHub.