egametang/ET · error · Exception

from bson error: {type.FullName} {bytes.Length}

Error message

from bson error: {type.FullName} {bytes.Length}

What it means

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').

Source

Thrown at Packages/cn.etetet.core/Scripts/Core/Share/Serialize/MongoHelper.cs:127

                args.NominalType = typeof (object);
                IBsonSerializer serializer = BsonSerializer.LookupSerializer(args.NominalType);
                serializer.Serialize(context, args, message);
            }
            catch (Exception e)
            {
                throw new Exception($"Serialize error {message.GetType().FullName}\n{e}");
            }
        }

        public static object Deserialize(Type type, byte[] bytes)
        {
            try
            {
                return BsonSerializer.Deserialize(bytes, type);
            }
            catch (Exception e)
            {
                throw new Exception($"from bson error: {type.FullName} {bytes.Length}", e);
            }
        }

        public static object Deserialize(Type type, byte[] bytes, int index, int count)
        {
            try
            {
                using MemoryStream memoryStream = new(bytes, index, count);
                
                return BsonSerializer.Deserialize(memoryStream, type);
            }
            catch (Exception e)
            {
                throw new Exception($"from bson error: {type.FullName} {bytes.Length} {index} {count}", e);
            }
        }

        public static object Deserialize(Type type, Stream stream)

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Verify bytes is non-null and long enough to be a BSON document (min 5 bytes incl. trailing 0).
  2. Check the inner exception (preserved) for the exact decode failure.
  3. Ensure the type matches what produced the bytes (same class map / discriminator).
  4. Guard against partial frames at the transport layer before calling Deserialize.
Defensive patterns

Strategy: validation

Validate before calling

if (type == null) throw new ArgumentNullException(nameof(type));
if (bytes == null || bytes.Length < 5)
    throw new ArgumentException("invalid bson buffer", nameof(bytes));

Try / catch

try { return MongoHelper.Deserialize(type, bytes); }
catch (Exception e) { Log.Error($"Deserialize({type}) len={bytes?.Length} failed: {e}"); throw; }

Prevention

When it happens

Trigger: Passing bytes that are not valid BSON for the given type: partial frame, wrong-endian, trailing garbage, type with unregistered serializer, or null bytes.

Common situations: Network message decode after a partial read, reading corrupted Mongo documents, version skew where bytes were serialized as a different type, null byte arrays.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/2f2174f384e84f8e. Report an issue: GitHub.