egametang/ET · error · Exception

from bson error: {typeof (T).FullName} {bytes.Length}

Error message

from bson error: {typeof (T).FullName} {bytes.Length}

What it means

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.

Source

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

                return BsonSerializer.Deserialize(stream, type);
            }
            catch (Exception e)
            {
                throw new Exception($"from bson error: {type.FullName} {stream.Position} {stream.Length}", e);
            }
        }

        public static T Deserialize<T>(byte[] bytes)
        {
            try
            {
                using MemoryStream memoryStream = new(bytes);
                
                return (T)BsonSerializer.Deserialize(memoryStream, typeof (T));
            }
            catch (Exception e)
            {
                throw new Exception($"from bson error: {typeof (T).FullName} {bytes.Length}", e);
            }
        }

        public static T Deserialize<T>(byte[] bytes, int index, int count)
        {
            return (T)Deserialize(typeof (T), bytes, index, count);
        }

        public static T Clone<T>(T t)
        {
            return Deserialize<T>(Serialize(t));
        }
    }
}

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Null/length-check bytes first (>= 5 bytes).
  2. Inspect the preserved inner exception.
  3. Ensure T's serializer/class map is registered and matches the writer.
  4. Buffer complete frames before decoding.
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

static bool CanDeserialize<T>(byte[] bytes) => bytes != null && bytes.Length >= 5 && BsonSerializer.IsTypeRegistered(typeof(T));

Try / catch

try { return MongoHelper.Deserialize<T>(bytes); }
catch (Exception e) { Log.Error($"Deserialize<{typeof(T).Name}> len={bytes?.Length} failed: {e}"); throw; }

Prevention

When it happens

Trigger: Bytes not a valid BSON document for T, truncated buffer, null bytes, or T unregistered.

Common situations: Decoding typed network messages, loading saved T state, version skew where bytes came from a different T, null buffers.

Related errors


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