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

  1. Verify all part writes completed before Close and Close is called exactly once
  2. Inspect the wrapped inner error for buffer/allocation causes
  3. 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

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


AI-assisted analysis of anomalyco/sst@a0bd20f762 (2026-08-30). Data as JSON: /api/errors/faef937fc1b5ff47. Report an issue: GitHub.