{"record":{"id":"2ef94b4d091c9274","repo":"dutchcoders/transfer.sh","slug":"could-not-encode-metadata-2ef94b","errorCode":null,"errorMessage":"Could not encode metadata","messagePattern":"Could not encode metadata","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"server/handlers.go","lineNumber":522,"sourceCode":"\t\t\t\tif err != nil {\n\t\t\t\t\ts.logger.Printf(\"%s\", err.Error())\n\t\t\t\t\thttp.Error(w, \"Could not perform prescan\", http.StatusInternalServerError)\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tif status != clamavScanStatusOK {\n\t\t\t\t\ts.logger.Printf(\"prescan positive: %s\", status)\n\t\t\t\t\thttp.Error(w, \"Clamav prescan found a virus\", http.StatusPreconditionFailed)\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tmetadata := metadataForRequest(contentType, contentLength, s.randomTokenLength, r)\n\n\t\t\tbuffer := &bytes.Buffer{}\n\t\t\tif err := json.NewEncoder(buffer).Encode(metadata); err != nil {\n\t\t\t\ts.logger.Printf(\"%s\", err.Error())\n\t\t\t\thttp.Error(w, \"Could not encode metadata\", http.StatusInternalServerError)\n\n\t\t\t\treturn\n\t\t\t} else if err := s.storage.Put(r.Context(), token, fmt.Sprintf(\"%s.metadata\", filename), buffer, \"text/json\", uint64(buffer.Len())); err != nil {\n\t\t\t\ts.logger.Printf(\"%s\", err.Error())\n\t\t\t\thttp.Error(w, \"Could not save metadata\", http.StatusInternalServerError)\n\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\ts.logger.Printf(\"Uploading %s %s %d %s\", token, filename, contentLength, contentType)\n\n\t\t\treader, err := attachEncryptionReader(file, r.Header.Get(\"X-Encrypt-Password\"))\n\t\t\tif err != nil {\n\t\t\t\thttp.Error(w, \"Could not crypt file\", http.StatusInternalServerError)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tif err = s.storage.Put(r.Context(), token, filename, reader, contentType, uint64(contentLength)); err != nil {","sourceCodeStart":504,"sourceCodeEnd":540,"githubUrl":"https://github.com/dutchcoders/transfer.sh/blob/c37bfd95797fd6da8a6da53fc13d191994b3f687/server/handlers.go#L504-L540","documentation":"postHandler builds a metadata struct (content type, length, download limits, deletion token, encryption flag) and encodes it as JSON into a buffer before storing it as `<filename>.metadata`. If json.Encoder.Encode returns an error, the server responds with 500 and this message. In practice this is nearly unreachable: the metadata struct contains only JSON-serializable types, so the encoder essentially never fails.","triggerScenarios":"json.NewEncoder(buffer).Encode(metadata) returns an error — theoretically only if a value in the metadata struct becomes unserializable (e.g. a custom type with a broken MarshalJSON); not triggerable by any client request input.","commonSituations":"Seen only when the code has been modified to put unsupported types (channels, funcs, invalid UTF-8 handled by custom marshalers) into the metadata struct; stock deployments never produce this.","solutions":["Inspect the server log printed just before the response; it contains the underlying encoding error.","If you modified the metadata struct, remove or fix the unserializable field (add a MarshalJSON or drop the field).","Upgrade to a stock release of the server where this path is effectively dead code.","Retry the upload — no client-side state is corrupted, nothing was written to storage."],"exampleFix":"// before: unserializable field added to metadata\n//   Files []os.File\n// after: store only serializable data\n//   FileNames []string","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"resp, err := http.Post(url, mime, body)\nif err == nil && resp.StatusCode == http.StatusInternalServerError &&\n    strings.Contains(readBody(resp), \"Could not encode metadata\") {\n    return fmt.Errorf(\"server-side metadata encoding bug; report to operator\")\n}","preventionTips":["Run stock server releases — this path is dead code upstream.","If you maintain the server, keep the metadata struct JSON-serializable.","Log the underlying encode error server-side for diagnosis.","Retry is safe; nothing was persisted."],"tags":["json","encoding","metadata","server","http"],"backgroundTag":"json-encode-failed","analyzedSha":"c37bfd95797fd6da8a6da53fc13d191994b3f687","analyzedAt":"2026-09-05T10:21:07.548Z","contentChangedAt":"2026-09-05T10:21:07.548Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}