egametang/ET · error · Exception

from bson error

Error message

from bson error: {type.FullName} {stream.Position} {stream.Length}

What it means

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.

Solutions

  1. Ensure stream.Position points at the document start and the stream contains at least one full document.
  2. Buffer/await until the full document length (from its 4-byte prefix) is available.
  3. Check the inner exception; register the type's serializer if missing.
  4. Prefer the byte[] overload when you already have buffered bytes.
Defensive patterns

Strategy: validation

Validate before calling

if (type == null) throw new ArgumentNullException(nameof(type));
if (stream == null || !stream.CanRead) throw new ArgumentException("stream not readable", nameof(stream));
if (stream.Position >= stream.Length) throw new EndOfStreamException("stream empty at position");

Try / catch

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

Prevention

When it happens

Trigger: Stream position not at the start of a document, stream shorter than one BSON document, type mismatch, or a non-readable stream.

Common situations: Deserializing from a NetworkStream before the full document arrived, reusing a stream without rewinding, Mongo cursor reads, decompressed-buffer reads with wrong position.

Related errors


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

Appendix: source

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

                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)
        {
            try
            {
                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)

View on GitHub (pinned to 5cab01f7a8)