Skip to content

Commit cdfafe5

Browse files
committed
Ensure collection.aggregate throws on error
JAVA-1253
1 parent 937d1f1 commit cdfafe5

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,7 @@ public AggregationOutput aggregate(final List<DBObject> pipeline, ReadPreference
15521552
DBObject command = prepareCommand(pipeline, options);
15531553

15541554
CommandResult res = _db.command(command, getOptions(), readPreference);
1555+
res.throwOnError();
15551556

15561557
return new AggregationOutput(command, res);
15571558
}

src/test/com/mongodb/AggregationTest.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void testAggregation() {
5959

6060
@Test
6161
public void testOldAggregationWithOut() {
62-
checkServerVersion(2.5);
62+
checkServerVersion(2.6);
6363
List<DBObject> pipeline = new ArrayList<DBObject>(buildPipeline());
6464
pipeline.add(new BasicDBObject("$out", "aggCollection"));
6565
final AggregationOutput out = collection.aggregate(pipeline);
@@ -70,7 +70,7 @@ public void testOldAggregationWithOut() {
7070

7171
@Test
7272
public void testExplain() {
73-
checkServerVersion(2.5);
73+
checkServerVersion(2.6);
7474
List<DBObject> pipeline = new ArrayList<DBObject>(buildPipeline());
7575
pipeline.add(new BasicDBObject("$out", "aggCollection"));
7676
final CommandResult out = collection.explainAggregate(pipeline, AggregationOptions.builder()
@@ -126,7 +126,7 @@ private List<DBObject> buildPipeline() {
126126

127127
@Test
128128
public void testAggregationCursor() {
129-
checkServerVersion(2.5);
129+
checkServerVersion(2.6);
130130
final List<DBObject> pipeline = prepareData();
131131

132132
verify(pipeline, AggregationOptions.builder()
@@ -149,7 +149,7 @@ public void testAggregationCursor() {
149149

150150
@Test
151151
public void testInlineAndDollarOut() {
152-
checkServerVersion(2.5);
152+
checkServerVersion(2.6);
153153
String aggCollection = "aggCollection";
154154
database.getCollection(aggCollection)
155155
.drop();
@@ -168,7 +168,7 @@ public void testInlineAndDollarOut() {
168168

169169
@Test
170170
public void testDollarOut() {
171-
checkServerVersion(2.5);
171+
checkServerVersion(2.6);
172172
String aggCollection = "aggCollection";
173173
database.getCollection(aggCollection)
174174
.drop();
@@ -185,7 +185,7 @@ public void testDollarOut() {
185185

186186
@Test
187187
public void testDollarOutOnSecondary() throws UnknownHostException {
188-
checkServerVersion(2.5);
188+
checkServerVersion(2.6);
189189
assumeTrue(isReplicaSet(cleanupMongo));
190190

191191
ServerAddress primary = new ServerAddress("localhost");
@@ -207,7 +207,7 @@ public void testDollarOutOnSecondary() throws UnknownHostException {
207207
@Test
208208
@Ignore
209209
public void testAggregateOnSecondary() throws UnknownHostException {
210-
checkServerVersion(2.5);
210+
checkServerVersion(2.6);
211211
assumeTrue(isReplicaSet(cleanupMongo));
212212

213213
ServerAddress primary = new ServerAddress("localhost");
@@ -229,7 +229,7 @@ public void testAggregateOnSecondary() throws UnknownHostException {
229229
@Test
230230
public void testMaxTime() {
231231
assumeFalse(isSharded(getMongoClient()));
232-
checkServerVersion(2.5);
232+
checkServerVersion(2.6);
233233
enableMaxTimeFailPoint();
234234
DBCollection collection = database.getCollection("testMaxTime");
235235
try {
@@ -242,6 +242,26 @@ public void testMaxTime() {
242242
}
243243
}
244244

245+
@Test
246+
public void testInvalidPipelineThrowsError() {
247+
checkServerVersion(2.6);
248+
DBCollection collection = database.getCollection("testInvalidPipeline");
249+
List<DBObject> invalidPipeline = asList((DBObject) new BasicDBObject("name", "foo"));
250+
try {
251+
collection.aggregate(invalidPipeline);
252+
fail("Show have thrown");
253+
} catch (CommandFailureException e) {
254+
// continue
255+
}
256+
257+
try {
258+
collection.aggregate(invalidPipeline, AggregationOptions.builder().build());
259+
fail("Show have thrown");
260+
} catch (CommandFailureException e) {
261+
// continue
262+
}
263+
}
264+
245265
public List<DBObject> prepareData() {
246266
collection.remove(new BasicDBObject());
247267

0 commit comments

Comments
 (0)