{"record":{"id":"eae39f389b7a0759","repo":"egametang/ET","slug":"from-bson-error-type-fullname-stream-position","errorCode":null,"errorMessage":"from bson error: {type.FullName} {stream.Position} {stream.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":153,"sourceCode":"                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)\n        {\n            try\n            {\n                return BsonSerializer.Deserialize(stream, type);\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"from bson error: {type.FullName} {stream.Position} {stream.Length}\", e);\n            }\n        }\n\n        public static T Deserialize<T>(byte[] bytes)\n        {\n            try\n            {\n                using MemoryStream memoryStream = new(bytes);\n                \n                return (T)BsonSerializer.Deserialize(memoryStream, typeof (T));\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"from bson error: {typeof (T).FullName} {bytes.Length}\", e);\n            }\n        }\n\n        public static T Deserialize<T>(byte[] bytes, int index, int count)","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs#L135-L171","documentation":"Thrown by MongoHelper.Deserialize(Type, Stream) (line 153) wrapping BsonSerializer.Deserialize(stream, type). Fails when the stream does not contain a complete/valid BSON document for the type, the stream position is wrong, or the type is unregistered. Inner exception IS preserved.","triggerScenarios":"Stream position not at the start of a document, stream shorter than one BSON document, type mismatch, or a non-readable stream.","commonSituations":"Deserializing from a NetworkStream before the full document arrived, reusing a stream without rewinding, Mongo cursor reads, decompressed-buffer reads with wrong position.","solutions":["Ensure stream.Position points at the document start and the stream contains at least one full document.","Buffer/await until the full document length (from its 4-byte prefix) is available.","Check the inner exception; register the type's serializer if missing.","Prefer the byte[] overload when you already have buffered bytes."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (type == null) throw new ArgumentNullException(nameof(type));\nif (stream == null || !stream.CanRead) throw new ArgumentException(\"stream not readable\", nameof(stream));\nif (stream.Position >= stream.Length) throw new EndOfStreamException(\"stream empty at position\");","typeGuard":null,"tryCatchPattern":"try { return MongoHelper.Deserialize(type, stream); }\ncatch (Exception e) { Log.Error($\"Deserialize({type}) stream pos={stream.Position} len={stream.Length} failed: {e}\"); throw; }","preventionTips":["Do not deserialize from a stream until the whole document is buffered.","Reset stream.Position to the document start before each call.","Register the target type's serializer."],"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"}