MongoUtils { static Type getDocumentStoreExceptionTypeFor(Throwable t) { Type type = Type.GENERIC; if (t instanceof MongoSocketException || t instanceof MongoWriteConcernException || t instanceof MongoNotPrimaryException) { type = Type.TRANSIENT; } else if (t instanceof MongoCommandException || t instanceof WriteConcernException) { int code = ((MongoException) t).getCode(); if (code == 11600 || code == 11601 || code == 11602) { type = Type.TRANSIENT; } } return type; }    }
@Test public void documentStoreExceptionType() { assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(new IOException())); assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(new MongoException("message"))); assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(newMongoCommandException(42))); assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(new DuplicateKeyException(response(11000), new ServerAddress(), null))); assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newWriteConcernException(11600))); assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newWriteConcernException(11601))); assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newWriteConcernException(11602))); assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newMongoCommandException(11600))); assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newMongoCommandException(11601))); assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newMongoCommandException(11602))); assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(new MongoSocketException("message", new ServerAddress()))); }