{"record":{"id":"308ee7b3be516af8","repo":"anomalyco/sst","slug":"failed-to-write-encoded-content-for-s-w","errorCode":null,"errorMessage":"failed to write encoded content for %s: %w","messagePattern":"failed to write encoded content for (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/server/resource/cloudflare-worker-assets.go","lineNumber":254,"sourceCode":"\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to read file %s: %w\", absFilePath, err)\n\t\t}\n\t\t\n\t\t// Base64 encode the file content\n\t\tencodedContent := base64.StdEncoding.EncodeToString(fileContent)\n\t\t\n\t\t// Create form field with content type header\n\t\tpart, err := writer.CreatePart(map[string][]string{\n\t\t\t\"Content-Disposition\": []string{fmt.Sprintf(`form-data; name=\"%s\"; filename=\"%s\"`, hash, hash)},\n\t\t\t\"Content-Type\":        []string{contentType},\n\t\t})\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to create form part %s: %w\", hash, err)\n\t\t}\n\n\t\t_, err = part.Write([]byte(encodedContent))\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to write encoded content for %s: %w\", hash, err)\n\t\t}\n\t}\n\terr := writer.Close()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to close writer: %w\", err)\n\t}\n\n\turl := fmt.Sprintf(\"https://api.cloudflare.com/client/v4/accounts/%s/workers/assets/upload?base64=true\", accountId)\n\n\treq, err := http.NewRequest(\"POST\", url, &body)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treq.Header.Set(\"Content-Type\", \"multipart/form-data; boundary=\"+writer.Boundary())\n\treq.Header.Set(\"Authorization\", \"Bearer \"+jwt)\n\n\tclient := &http.Client{Timeout: 30 * time.Second}\n\tresp, err := client.Do(req)","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/anomalyco/sst/blob/a0bd20f762883e72a35caccb4896c42ce5b3f707/pkg/server/resource/cloudflare-worker-assets.go#L236-L272","documentation":"After creating the form part, uploadAssets writes the base64-encoded asset content into it. This error wraps a write failure on the multipart writer; since the underlying writer is an in-memory bytes.Buffer, it only fails on extremely rare internal multipart errors (e.g. writing after the writer was closed or a corrupted part).","triggerScenarios":"part.Write([]byte(encodedContent)) returns non-nil err inside the asset loop in uploadAssets (pkg/server/resource/cloudflare-worker-assets.go:254); practically only when the multipart.Writer is in an invalid state (closed early, buffer exhausted/failed).","commonSituations":"Reusing or closing the multipart writer before all parts are written; memory pressure causing buffer failures in constrained environments.","solutions":["Ensure no code path closes the writer before all parts are written","Check errors.Unwrap(err) for the underlying cause","Write to a fresh bytes.Buffer/multipart.Writer for this upload","If contents can be huge, stream to a temp file instead of an in-memory buffer"],"exampleFix":"// before\nvar body bytes.Buffer\nwriter := multipart.NewWriter(&body)\n// ... later reuse after Close() ...\n_, err = part.Write([]byte(encodedContent))\n// after\nvar body bytes.Buffer\nwriter := multipart.NewWriter(&body)\n// write ALL parts, then Close() exactly once at the end\n_, err = part.Write([]byte(encodedContent))\nif err != nil {\n\treturn \"\", fmt.Errorf(\"failed to write encoded content for %s: %w\", hash, err)\n}","handlingStrategy":"try-catch","validationCode":"if body.Len() > maxInMemoryBodySize {\n\treturn fmt.Errorf(\"asset upload body too large for in-memory buffer\")\n}","typeGuard":null,"tryCatchPattern":"if _, err := part.Write([]byte(encodedContent)); err != nil {\n\treturn \"\", fmt.Errorf(\"failed to write encoded content for %s: %w\", hash, err)\n}","preventionTips":["Write all parts before calling Close, and Close exactly once","Keep payload sizes in mind; stream large asset sets to a temp file","Check the underlying buffer writer for earlier silent errors"],"tags":["cloudflare","multipart","workers-assets","io"],"backgroundTag":"multipart-write-failed","analyzedSha":"a0bd20f762883e72a35caccb4896c42ce5b3f707","analyzedAt":"2026-08-30T11:26:00.383Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}