{"record":{"id":"455f46acbc6dcfc8","repo":"egametang/ET","slug":"from-bson-error-typeof-t-fullname-bytes-leng","errorCode":null,"errorMessage":"from bson error: {typeof (T).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":167,"sourceCode":"                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)\n        {\n            return (T)Deserialize(typeof (T), bytes, index, count);\n        }\n\n        public static T Clone<T>(T t)\n        {\n            return Deserialize<T>(Serialize(t));\n        }\n    }\n}","sourceCodeStart":149,"sourceCodeEnd":181,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs#L149-L181","documentation":"Generic overload MongoHelper.Deserialize<T>(byte[]) (line 167) wrapping BsonSerializer.Deserialize over a MemoryStream(bytes). Same failure modes as error 146 but typed. Inner exception IS preserved.","triggerScenarios":"Bytes not a valid BSON document for T, truncated buffer, null bytes, or T unregistered.","commonSituations":"Decoding typed network messages, loading saved T state, version skew where bytes came from a different T, null buffers.","solutions":["Null/length-check bytes first (>= 5 bytes).","Inspect the preserved inner exception.","Ensure T's serializer/class map is registered and matches the writer.","Buffer complete frames before decoding."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (bytes == null || bytes.Length < 5)\n    throw new ArgumentException(\"invalid bson buffer\", nameof(bytes));","typeGuard":"static bool CanDeserialize<T>(byte[] bytes) => bytes != null && bytes.Length >= 5 && BsonSerializer.IsTypeRegistered(typeof(T));","tryCatchPattern":"try { return MongoHelper.Deserialize<T>(bytes); }\ncatch (Exception e) { Log.Error($\"Deserialize<{typeof(T).Name}> len={bytes?.Length} failed: {e}\"); throw; }","preventionTips":["Buffer full frames before decoding.","Keep T class-map versions aligned across writer/reader.","Register T at startup."],"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"}