{"record":{"id":"20241ca69cec75c7","repo":"egametang/ET","slug":"serialize-error-obj-gettype-fullname-e","errorCode":null,"errorMessage":"Serialize error {obj.GetType().FullName}\n{e}","messagePattern":"Serialize error (.+?)\n(.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs","lineNumber":92,"sourceCode":"            catch (Exception e)\n            {\n                throw new Exception($\"from json error: {str}\\n{e}\");\n            }\n        }\n\n        public static byte[] Serialize(object obj)\n        {\n            try\n            {\n                if (obj is ISupportInitialize supportInitialize)\n                {\n                    supportInitialize.BeginInit();\n                }\n                return obj.ToBson();\n            }\n            catch (Exception e)\n            {\n                throw new Exception($\"Serialize error {obj.GetType().FullName}\\n{e}\");\n            }\n        }\n\n        public static void Serialize(object message, MemoryStream stream)\n        {\n            try\n            {\n                if (message is ISupportInitialize supportInitialize)\n                {\n                    supportInitialize.BeginInit();\n                }\n\n                using BsonBinaryWriter bsonWriter = new(stream, BsonBinaryWriterSettings.Defaults);\n            \n                BsonSerializationContext context = BsonSerializationContext.CreateRoot(bsonWriter);\n                BsonSerializationArgs args = default;\n                args.NominalType = typeof (object);\n                IBsonSerializer serializer = BsonSerializer.LookupSerializer(args.NominalType);","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs#L74-L110","documentation":"Thrown by MongoHelper.Serialize(object) (line 92) wrapping obj.ToBson() failures. ToBson requires every member to have a BSON serializer; any gap (interface-typed field, unregistered concrete type, missing class map) raises here. Inner exception is NOT preserved (no ', e'), so only the formatted message (type FullName + inner text) is available.","triggerScenarios":"Serializing an object graph containing a member whose type lacks a registered BSON serializer, a polymorphic field without [BsonKnownTypes]/discriminator, an unsupported type (e.g. System.Type, delegates), or null obj (catch NREs on obj.GetType()).","commonSituations":"Network message serialization (the hot path for ET messages), saving entities to Mongo, adding a new field of an uncommon type, null after entity disposal.","solutions":["From the message identify the failing type, then register its serializer/class map at startup.","Add [BsonKnownTypes] / [BsonDiscriminator] to polymorphic members so ToBson can pick the concrete serializer.","Avoid non-serializable members (interfaces, delegates, IntPtr); mark them [BsonIgnore].","Null-check obj first.","Patch helper to preserve InnerException."],"exampleFix":"// before\nbyte[] data = MongoHelper.Serialize(msg);\n\n// after\n[BsonIgnoreExtraElements]\npublic class MyMsg\n{\n    public long Id;\n    [BsonIgnore] public Action Callback; // excluded from bson\n}\nif (msg != null) data = MongoHelper.Serialize(msg);","handlingStrategy":"validation","validationCode":"if (obj == null) throw new ArgumentNullException(nameof(obj));\nif (!BsonSerializer.IsTypeRegistered(obj.GetType()))\n    throw new InvalidOperationException($\"no bson serializer for {obj.GetType().FullName}\");","typeGuard":null,"tryCatchPattern":"try { return MongoHelper.Serialize(obj); }\ncatch (Exception e) { Log.Error($\"Serialize {obj?.GetType()} failed: {e}\"); throw; }","preventionTips":["Register all message/entity BsonClassMaps at startup.","Mark non-serializable fields [BsonIgnore].","Never pass null (catch block NREs)."],"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"}