{"record":{"id":"1f079738503a771b","repo":"XTLS/Xray-core","slug":"write-packet-field-w","errorCode":null,"errorMessage":"write packet field: %w","messagePattern":"write packet field: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"transport/internet/finalmask/xmc/protocol.go","lineNumber":400,"sourceCode":"\nfunc writePacketWithLength(w io.Writer, packetID int, fields ...field) (int, error) {\n\tframe, err := encodePacket(packetID, fields...)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\tif err = writeFull(w, frame); err != nil {\n\t\treturn 0, fmt.Errorf(\"write packet data: %w\", err)\n\t}\n\treturn len(frame), nil\n}\n\nfunc encodePacket(packetID int, fields ...field) ([]byte, error) {\n\tvar dataBuf bytes.Buffer\n\n\tfor _, field := range fields {\n\t\terr := field.writeTo(&dataBuf)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"write packet field: %w\", err)\n\t\t}\n\t}\n\tif dataBuf.Len() > maxPacketDataLength {\n\t\treturn nil, fmt.Errorf(\"write packet: bad length: %d\", dataBuf.Len())\n\t}\n\n\tpacketIDVarint := Varint(packetID)\n\tbodyLength := varintSize(packetIDVarint) + dataBuf.Len()\n\tif bodyLength > maxPacketBodyLength {\n\t\treturn nil, fmt.Errorf(\"write packet: bad length: %d\", bodyLength)\n\t}\n\n\tvar frame bytes.Buffer\n\tframe.Grow(varintSize(Varint(bodyLength)) + bodyLength)\n\tframeLength := Varint(bodyLength)\n\tif err := frameLength.writeTo(&frame); err != nil {\n\t\treturn nil, fmt.Errorf(\"write packet length: %w\", err)\n\t}","sourceCodeStart":382,"sourceCodeEnd":418,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/transport/internet/finalmask/xmc/protocol.go#L382-L418","documentation":"Returned by encodePacket when a single field's writeTo fails while serializing fields into an in-memory bytes.Buffer prior to framing. Because bytes.Buffer writes never return errors, this path is only reachable if a field type implements writeTo with its own failure logic. In practice it is a defensive guard inside the Minecraft packet encoder used by the xmc transport.","triggerScenarios":"Calling writePacket/encodePacket with any field whose writeTo returns an error during serialization into the dataBuf bytes.Buffer in transport/internet/finalmask/xmc/protocol.go:400.","commonSituations":"Almost never seen in production; would only surface from a custom field type or future codec changes introducing fallible writes. Encountered mainly in tests that inject failing field implementations.","solutions":["Inspect the wrapped error (%w) to identify which field type failed to serialize","Verify you are not passing a custom field implementation with a fallible writeTo","If unreachable in your code path, treat as an internal invariant violation and report upstream"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if _, err := writePacket(w, id, fields...); err != nil {\\n\\tif strings.Contains(err.Error(), \"write packet field\") {\\n\\t\\t// serialization invariant failure: inspect err via errors.Unwrap\\n\\t\\tlog.Debug(\"xmc: field serialization failed: \", err)\\n\\t}\\n\\treturn err\\n}","preventionTips":["Use only the stock field types (Varint, String, Bytes, ...) when composing packets","Treat occurrences as a bug to report, not a runtime condition to handle"],"tags":["go","minecraft-protocol","serialization","xmc"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}