Skip to content

Commit 4c2b8da

Browse files
author
Trisha Gee
committed
Added missing 2.x Exceptions and the mappings for them.
1 parent 7f602cd commit 4c2b8da

10 files changed

Lines changed: 227 additions & 70 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb;
18+
19+
public class ConnectionWaitTimeOut extends NoMoreConnection {
20+
private static final long serialVersionUID = -4415279469780082174L;
21+
22+
ConnectionWaitTimeOut(final String message) {
23+
super(message);
24+
}
25+
}

driver-compat/src/main/com/mongodb/MongoExceptions.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import org.mongodb.MongoInterruptedException;
2020
import org.mongodb.command.MongoCommandFailureException;
2121
import org.mongodb.command.MongoDuplicateKeyException;
22+
import org.mongodb.command.MongoWriteConcernException;
2223
import org.mongodb.connection.MongoSocketException;
24+
import org.mongodb.connection.MongoTimeoutException;
25+
import org.mongodb.connection.MongoWaitQueueFullException;
2326
import org.mongodb.operation.MongoCursorNotFoundException;
2427
import org.mongodb.operation.ServerCursor;
2528

@@ -30,11 +33,19 @@ public static com.mongodb.MongoException mapException(final org.mongodb.MongoExc
3033
final Throwable cause = e.getCause();
3134
if (e instanceof MongoDuplicateKeyException) {
3235
return new MongoException.DuplicateKey((MongoDuplicateKeyException) e);
33-
} else if (e instanceof MongoCommandFailureException) {
34-
return new CommandFailureException(new CommandResult(((MongoCommandFailureException) e).getCommandResult()), e.getMessage());
36+
} else if (e instanceof MongoWriteConcernException) {
37+
return new WriteConcernException(new CommandResult(((MongoCommandFailureException) e).getCommandResult()), e.getMessage());
38+
} else if (e instanceof org.mongodb.MongoInternalException) {
39+
return new MongoInternalException(e.getMessage(), e.getCause());
40+
} else if (e instanceof MongoTimeoutException) {
41+
return new ConnectionWaitTimeOut(e.getMessage());
42+
} else if (e instanceof MongoWaitQueueFullException) {
43+
return new SemaphoresOut(e.getMessage());
3544
} else if (e instanceof MongoCursorNotFoundException) {
3645
final ServerCursor serverCursor = ((MongoCursorNotFoundException) e).getCursor();
3746
return new MongoException.CursorNotFound(serverCursor.getId(), new ServerAddress(serverCursor.getAddress()));
47+
} else if (e instanceof MongoCommandFailureException) {
48+
return new CommandFailureException(new CommandResult(((MongoCommandFailureException) e).getCommandResult()), e.getMessage());
3849
} else if ((e instanceof MongoSocketException || e instanceof MongoInterruptedException) && cause instanceof IOException) {
3950
return new MongoException.Network(e.getMessage(), (IOException) cause);
4051
}

driver-compat/src/main/com/mongodb/MongoInternalException.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
/**
2222
* A Mongo exception internal to the driver, not carrying any error code.
23-
*
24-
* @author antoine
2523
*/
2624
public class MongoInternalException extends MongoException {
2725

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb;
18+
19+
public class NoMoreConnection extends MongoInternalException {
20+
private static final long serialVersionUID = -4415279469780082174L;
21+
22+
NoMoreConnection(final String msg) {
23+
super(msg);
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb;
18+
19+
public class SemaphoresOut extends NoMoreConnection {
20+
private static final long serialVersionUID = -4415279469780082174L;
21+
22+
SemaphoresOut(final String message) {
23+
super(message);
24+
}
25+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb;
18+
19+
/**
20+
* An exception representing an error reported due to a write failure.
21+
*/
22+
public class WriteConcernException extends MongoException {
23+
private static final long serialVersionUID = 841056799207039974L;
24+
25+
private final CommandResult commandResult;
26+
27+
/**
28+
* Construct a new instance with the CommandResult from getlasterror command
29+
*
30+
* @param commandResult the command result
31+
*/
32+
public WriteConcernException(final CommandResult commandResult) {
33+
this(commandResult, commandResult.toString());
34+
}
35+
36+
public WriteConcernException(final CommandResult commandResult, final String message) {
37+
super(ServerError.getCode(commandResult), message);
38+
this.commandResult = commandResult;
39+
}
40+
41+
/**
42+
* Gets the getlasterror command result document.
43+
*
44+
* @return the command result
45+
*/
46+
public CommandResult getCommandResult() {
47+
return commandResult;
48+
}
49+
}

driver-compat/src/test/unit/com/mongodb/MongoExceptionsSpecification.groovy

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import org.mongodb.Document
2020
import org.mongodb.MongoInterruptedException
2121
import org.mongodb.command.MongoCommandFailureException
2222
import org.mongodb.command.MongoDuplicateKeyException
23+
import org.mongodb.command.MongoWriteConcernException
2324
import org.mongodb.connection.MongoSocketReadException
25+
import org.mongodb.connection.MongoTimeoutException
26+
import org.mongodb.connection.MongoWaitQueueFullException
2427
import org.mongodb.operation.MongoCursorNotFoundException
2528
import org.mongodb.operation.ServerCursor
2629
import spock.lang.Specification
@@ -41,6 +44,7 @@ class MongoExceptionsSpecification extends Specification {
4144
actualException instanceof MongoException.Network
4245
actualException.getCause() == cause
4346
actualException.getMessage() == expectedMessage
47+
assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
4448
}
4549

4650
def 'should convert InterruptedExceptions that are not InterruptedIOExceptions into MongoException'() {
@@ -56,6 +60,7 @@ class MongoExceptionsSpecification extends Specification {
5660
actualException instanceof MongoException
5761
actualException.getCause() == cause
5862
actualException.getMessage() == expectedMessage
63+
assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
5964
}
6065

6166
def 'should convert IOExceptions into MongoException.Network'() {
@@ -71,6 +76,7 @@ class MongoExceptionsSpecification extends Specification {
7176
actualException instanceof MongoException.Network
7277
actualException.getCause() == cause
7378
actualException.getMessage() == expectedMessage
79+
assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
7480
}
7581

7682
def 'should convert SocketExceptions that are not IOExceptions into MongoException'() {
@@ -85,6 +91,7 @@ class MongoExceptionsSpecification extends Specification {
8591
!(actualException instanceof MongoException.Network)
8692
actualException instanceof MongoException
8793
actualException.getMessage() == expectedMessage
94+
assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
8895
}
8996

9097
def 'should convert MongoDuplicateKeyException into DuplicateKeyException'() {
@@ -93,19 +100,14 @@ class MongoExceptionsSpecification extends Specification {
93100
String expectedMessage = "Command failed with response { \"code\" : $expectedErrorCode } " +
94101
"on server ServerAddress{host='127.0.0.1', port=27017}"
95102

96-
def newStyleException = new MongoDuplicateKeyException(
97-
new org.mongodb.operation.CommandResult(new Document(),
98-
new org.mongodb.connection.ServerAddress(),
99-
new Document
100-
('code', expectedErrorCode),
101-
15L))
102103
when:
103-
MongoException actualException = mapException(newStyleException)
104+
MongoException actualException = mapException(new MongoDuplicateKeyException(commandResultWithErrorCode(expectedErrorCode)))
104105

105106
then:
106107
actualException instanceof MongoException.DuplicateKey
107108
actualException.getMessage() == expectedMessage
108109
actualException.getCode() == expectedErrorCode
110+
assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
109111
}
110112

111113
def 'should convert MongoCommandFailureException into CommandFailureException'() {
@@ -114,19 +116,15 @@ class MongoExceptionsSpecification extends Specification {
114116
String expectedMessage = "Command failed with response { \"code\" : $expectedErrorCode } " +
115117
"on server ServerAddress{host='127.0.0.1', port=27017}"
116118

117-
def newStyleException = new MongoCommandFailureException(
118-
new org.mongodb.operation.CommandResult(new Document(),
119-
new org.mongodb.connection.ServerAddress(),
120-
new Document
121-
('code', expectedErrorCode),
122-
15L))
119+
def newStyleException = new MongoCommandFailureException(commandResultWithErrorCode(expectedErrorCode))
123120
when:
124121
MongoException actualException = mapException(newStyleException)
125122

126123
then:
127124
actualException instanceof CommandFailureException
128125
actualException.getMessage() == expectedMessage
129126
actualException.getCode() == expectedErrorCode
127+
assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
130128
}
131129

132130
def 'should convert MongoCursorNotFoundException into MongoException.CursorNotFound'() {
@@ -145,56 +143,76 @@ class MongoExceptionsSpecification extends Specification {
145143
actualAsCursorNotFound.getCursorId() == cursorId
146144
actualAsCursorNotFound.getServerAddress().getHost() == serverAddress.getHost()
147145
actualAsCursorNotFound.getServerAddress().getPort() == serverAddress.getPort()
146+
147+
assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
148148
}
149149

150-
def 'should not expose org.mongodb Exception in the stack trace of MongoInterruptedException'() {
150+
def 'should convert org.mongodb.MongoInternalException into com.mongodb.MongoInternalException'() {
151151
given:
152-
String expectedMessage = 'Interrupted IO Exception in the new architecture'
153-
def cause = new InterruptedIOException('The cause')
152+
String expectedMessage = 'Internal Exception thrown'
154153

155154
when:
156-
MongoException actualException = mapException(new MongoInterruptedException(expectedMessage, cause))
155+
MongoException actualException = mapException(new org.mongodb.MongoInternalException(expectedMessage))
157156

158157
then:
159-
!(actualException.getCause() instanceof org.mongodb.MongoException)
160-
!actualException.getStackTrace().any { it.className.startsWith('org.mongodb') }
158+
actualException instanceof MongoInternalException
159+
actualException.getMessage() == expectedMessage
160+
assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
161161
}
162162

163-
def 'should not expose org.mongodb Exception in the stack trace of MongoCursorNotFoundException'() {
163+
def 'should convert MongoWriteConcernException into com.mongodb.WriteConcernException'() {
164+
given:
165+
int expectedErrorCode = 500
166+
String expectedMessage = "Command failed with response { \"code\" : $expectedErrorCode } " +
167+
"on server ServerAddress{host='127.0.0.1', port=27017}"
168+
164169
when:
165-
MongoException actualException = mapException(new MongoCursorNotFoundException(
166-
new ServerCursor(123L, new org.mongodb.connection.ServerAddress())))
170+
MongoException actualException = mapException(new MongoWriteConcernException(commandResultWithErrorCode(expectedErrorCode)))
167171

168172
then:
169-
!(actualException.getCause() instanceof org.mongodb.MongoException)
170-
!actualException.getStackTrace().any { it.className.startsWith('org.mongodb') }
173+
actualException instanceof WriteConcernException
174+
actualException.getMessage() == expectedMessage
175+
actualException.getCode() == expectedErrorCode
176+
assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
171177
}
172178

173-
def 'should not expose org.mongodb Exception in the stack trace of MongoCommandFailureException'() {
179+
def 'should convert MongoTimeoutException into com.mongodb.ConnectionWaitTimeOut'() {
180+
given:
181+
String expectedMessage = 'A timeout exception was throwm'
182+
174183
when:
175-
MongoException actualException = mapException(new MongoCommandFailureException(
176-
new org.mongodb.operation.CommandResult(new Document(),
177-
new org.mongodb.connection.ServerAddress(),
178-
new Document(),
179-
15L)))
184+
MongoException actualException = mapException(new MongoTimeoutException(expectedMessage))
180185

181186
then:
182-
!(actualException.getCause() instanceof org.mongodb.MongoException)
183-
!actualException.getStackTrace().any { it.className.startsWith('org.mongodb') }
187+
actualException instanceof ConnectionWaitTimeOut
188+
actualException.getMessage() == expectedMessage
189+
assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
184190
}
185191

186-
def 'should not expose org.mongodb Exception in the stack trace of MongoDuplicateKeyException'() {
187-
when:
192+
def 'should convert MongoWaitQueueFullException into com.mongodb.SemaphoresOut'() {
193+
given:
194+
String expectedMessage = 'A queue full exception was throwm'
188195

189-
MongoException actualException = mapException(new MongoDuplicateKeyException(
190-
new org.mongodb.operation.CommandResult(new Document(),
191-
new org.mongodb.connection.ServerAddress(),
192-
new Document(),
193-
15L)))
196+
when:
197+
MongoException actualException = mapException(new MongoWaitQueueFullException(expectedMessage))
194198

195199
then:
196-
!(actualException.getCause() instanceof org.mongodb.MongoException)
197-
!actualException.getStackTrace().any { it.className.startsWith('org.mongodb') }
200+
actualException instanceof SemaphoresOut
201+
actualException.getMessage() == expectedMessage
202+
assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
203+
}
204+
205+
private static org.mongodb.operation.CommandResult commandResultWithErrorCode(int expectedErrorCode) {
206+
new org.mongodb.operation.CommandResult(new Document(),
207+
new org.mongodb.connection.ServerAddress(),
208+
new Document
209+
('code', expectedErrorCode),
210+
15L)
211+
}
212+
213+
private static void assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(MongoException actualException) {
214+
assert !(actualException.getCause() instanceof org.mongodb.MongoException)
215+
assert !actualException.getStackTrace().any { it.className.startsWith('org.mongodb') }
198216
}
199217

200218
}

driver/src/main/org/mongodb/connection/impl/DefaultAsyncConnectionProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class DefaultAsyncConnectionProvider implements AsyncConnectionProvider {
3737

3838
private final ConcurrentPool<AsyncConnection> pool;
3939
private final DefaultConnectionProviderSettings settings;
40-
private AtomicInteger waitQueueSize = new AtomicInteger(0);
40+
private final AtomicInteger waitQueueSize = new AtomicInteger(0);
4141

4242
public DefaultAsyncConnectionProvider(final ServerAddress serverAddress, final AsyncConnectionFactory connectionFactory,
4343
final DefaultConnectionProviderSettings settings) {
@@ -64,7 +64,9 @@ public AsyncConnection get() {
6464
public AsyncConnection get(final long timeout, final TimeUnit timeUnit) {
6565
try {
6666
if (waitQueueSize.incrementAndGet() > settings.getMaxWaitQueueSize()) {
67-
throw new MongoWaitQueueFullException("Too many threads are already waiting for a connection");
67+
throw new MongoWaitQueueFullException(String.format("Too many threads are already waiting for a connection. "
68+
+ "Max number of threads (maxWaitQueueSize) of %d has been exceeded.",
69+
settings.getMaxWaitQueueSize()));
6870
}
6971
return wrap(pool.get(timeout, timeUnit));
7072
} finally {

0 commit comments

Comments
 (0)