Skip to content

Commit cadedb1

Browse files
committed
Ensure too large documents cause error on insert
GODRIVER-510 Change-Id: Ief2fa6b04df3dd98e7861b47aa3208db780a093d
1 parent 8df56f1 commit cadedb1

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

core/command/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ var (
2222
ErrMultiDocCommandResponse = errors.New("command returned multiple documents")
2323
// ErrNoDocCommandResponse occurs when the server indicated a response existed, but none was found.
2424
ErrNoDocCommandResponse = errors.New("command returned no documents")
25+
// ErrDocumentTooLarge occurs when a document that is larger than the maximum size accepted by a
26+
// server is passed to an insert command.
27+
ErrDocumentTooLarge = errors.New("an inserted document is too large")
2528
)
2629

2730
// QueryFailureError is an error representing a command failure as a document.

core/command/insert.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ splitInserts:
6464
return nil, err
6565
}
6666

67+
if int(itsize) > targetBatchSize {
68+
return nil, ErrDocumentTooLarge
69+
}
6770
if size+int(itsize) > targetBatchSize {
6871
break assembleBatch
6972
}

core/command/insert_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ func TestInsertCommandSplitting(t *testing.T) {
7878
}
7979

8080
})
81+
t.Run("document_larger_than_max_size", func(t *testing.T) {
82+
i := &Insert{}
83+
i.Docs = append(i.Docs, bson.NewDocument(bson.EC.String("a", "bcdefghijklmnopqrstuvwxyz")))
84+
_, err := i.split(100, 5)
85+
if err != ErrDocumentTooLarge {
86+
t.Errorf("Expected a too large error. got %v; want %v", err, ErrDocumentTooLarge)
87+
}
88+
})
8189
}

0 commit comments

Comments
 (0)