{"record":{"id":"2f2174f384e84f8e","repo":"egametang/ET","slug":"from-bson-error-type-fullname-bytes-length","errorCode":null,"errorMessage":"from bson error: {type.FullName} {bytes.Length}","messagePattern":"from bson error: (.+?) (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs","lineNumber":127,"sourceCode":"                args.NominalType = typeof (object);\n                IBsonSerializer serializer = BsonSerializer.LookupSerializer(args.NominalType);\n                serializer.Serialize(context, args, message);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"Serialize error {message.GetType().FullName}\\n{e}\");\n            }\n        }\n\n        public static object Deserialize(Type type, byte[] bytes)\n        {\n            try\n            {\n                return BsonSerializer.Deserialize(bytes, type);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"from bson error: {type.FullName} {bytes.Length}\", e);\n            }\n        }\n\n        public static object Deserialize(Type type, byte[] bytes, int index, int count)\n        {\n            try\n            {\n                using MemoryStream memoryStream = new(bytes, index, count);\n                \n                return BsonSerializer.Deserialize(memoryStream, type);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"from bson error: {type.FullName} {bytes.Length} {index} {count}\", e);\n            }\n        }\n\n        public static object Deserialize(Type type, Stream stream)","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs#L109-L145","documentation":"Thrown by MongoHelper.Deserialize(Type, byte[]) (line 127) when BsonSerializer.Deserialize(bytes, type) fails. Byte-level BSON deserialization breaks on truncated/corrupt bytes, a type mismatch, or an unregistered type. Inner exception IS preserved (', e').","triggerScenarios":"Passing bytes that are not valid BSON for the given type: partial frame, wrong-endian, trailing garbage, type with unregistered serializer, or null bytes.","commonSituations":"Network message decode after a partial read, reading corrupted Mongo documents, version skew where bytes were serialized as a different type, null byte arrays.","solutions":["Verify bytes is non-null and long enough to be a BSON document (min 5 bytes incl. trailing 0).","Check the inner exception (preserved) for the exact decode failure.","Ensure the type matches what produced the bytes (same class map / discriminator).","Guard against partial frames at the transport layer before calling Deserialize."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (type == null) throw new ArgumentNullException(nameof(type));\nif (bytes == null || bytes.Length < 5)\n    throw new ArgumentException(\"invalid bson buffer\", nameof(bytes));","typeGuard":null,"tryCatchPattern":"try { return MongoHelper.Deserialize(type, bytes); }\ncatch (Exception e) { Log.Error($\"Deserialize({type}) len={bytes?.Length} failed: {e}\"); throw; }","preventionTips":["Validate frame completeness before deserializing network bytes.","Keep writer and reader type/class-map versions in sync.","Register serializers for the target type."],"tags":["serialization","mongodb","bson","network"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}