4848import static com .mongodb .assertions .Assertions .notNull ;
4949import static com .mongodb .crypt .capi .MongoCryptContext .State ;
5050import static com .mongodb .internal .client .vault .EncryptOptionsHelper .asMongoExplicitEncryptOptions ;
51+ import static com .mongodb .internal .thread .InterruptionUtil .translateInterruptedException ;
5152
5253/**
5354 * <p>This class is not part of the public API and may be removed or changed at any time</p>
@@ -141,7 +142,7 @@ RawBsonDocument encrypt(final String databaseName, final RawBsonDocument command
141142 try (MongoCryptContext encryptionContext = mongoCrypt .createEncryptionContext (databaseName , command )) {
142143 return executeStateMachine (encryptionContext , databaseName );
143144 } catch (MongoCryptException e ) {
144- throw wrapInClientException (e );
145+ throw wrapInMongoException (e );
145146 }
146147 }
147148
@@ -156,7 +157,7 @@ RawBsonDocument decrypt(final RawBsonDocument commandResponse) {
156157 try (MongoCryptContext decryptionContext = mongoCrypt .createDecryptionContext (commandResponse )) {
157158 return executeStateMachine (decryptionContext , null );
158159 } catch (MongoCryptException e ) {
159- throw wrapInClientException (e );
160+ throw wrapInMongoException (e );
160161 }
161162 }
162163
@@ -179,7 +180,7 @@ BsonDocument createDataKey(final String kmsProvider, final DataKeyOptions option
179180 .build ())) {
180181 return executeStateMachine (dataKeyCreationContext , null );
181182 } catch (MongoCryptException e ) {
182- throw wrapInClientException (e );
183+ throw wrapInMongoException (e );
183184 }
184185 }
185186
@@ -198,7 +199,7 @@ BsonBinary encryptExplicitly(final BsonValue value, final EncryptOptions options
198199 new BsonDocument ("v" , value ), asMongoExplicitEncryptOptions (options ))) {
199200 return executeStateMachine (encryptionContext , null ).getBinary ("v" );
200201 } catch (MongoCryptException e ) {
201- throw wrapInClientException (e );
202+ throw wrapInMongoException (e );
202203 }
203204 }
204205
@@ -218,7 +219,7 @@ BsonDocument encryptExpression(final BsonDocument expression, final EncryptOptio
218219 new BsonDocument ("v" , expression ), asMongoExplicitEncryptOptions (options ))) {
219220 return executeStateMachine (encryptionContext , null ).getDocument ("v" );
220221 } catch (MongoCryptException e ) {
221- throw wrapInClientException (e );
222+ throw wrapInMongoException (e );
222223 }
223224 }
224225
@@ -233,7 +234,7 @@ BsonValue decryptExplicitly(final BsonBinary value) {
233234 try (MongoCryptContext decryptionContext = mongoCrypt .createExplicitDecryptionContext (new BsonDocument ("v" , value ))) {
234235 return assertNotNull (executeStateMachine (decryptionContext , null ).get ("v" ));
235236 } catch (MongoCryptException e ) {
236- throw wrapInClientException (e );
237+ throw wrapInMongoException (e );
237238 }
238239 }
239240
@@ -256,7 +257,7 @@ BsonDocument rewrapManyDataKey(final BsonDocument filter, final RewrapManyDataKe
256257 return executeStateMachine (rewrapManyDatakeyContext , null );
257258 }
258259 } catch (MongoCryptException e ) {
259- throw wrapInClientException (e );
260+ throw wrapInMongoException (e );
260261 }
261262 }
262263
@@ -324,7 +325,7 @@ private void mark(final MongoCryptContext cryptContext, final String databaseNam
324325 cryptContext .addMongoOperationResult (markedCommand );
325326 cryptContext .completeMongoOperation ();
326327 } catch (Throwable t ) {
327- throw wrapInClientException (t );
328+ throw wrapInMongoException (t );
328329 }
329330 }
330331
@@ -348,7 +349,8 @@ private void decryptKeys(final MongoCryptContext cryptContext) {
348349 }
349350 cryptContext .completeKeyDecryptors ();
350351 } catch (Throwable t ) {
351- throw wrapInClientException (t );
352+ throw translateInterruptedException (t , "Interrupted while doing IO" )
353+ .orElseThrow (() -> wrapInMongoException (t ));
352354 }
353355 }
354356
@@ -366,7 +368,11 @@ private void decryptKey(final MongoKeyDecryptor keyDecryptor) throws IOException
366368 }
367369 }
368370
369- private MongoClientException wrapInClientException (final Throwable t ) {
370- return new MongoClientException ("Exception in encryption library: " + t .getMessage (), t );
371+ private MongoException wrapInMongoException (final Throwable t ) {
372+ if (t instanceof MongoException ) {
373+ return (MongoException ) t ;
374+ } else {
375+ return new MongoClientException ("Exception in encryption library: " + t .getMessage (), t );
376+ }
371377 }
372378}
0 commit comments