spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.62k stars 1.09k forks source link

Translation of WriteConcernExceptions [DATAMONGO-1451] #2362

Open spring-projects-issues opened 8 years ago

spring-projects-issues commented 8 years ago

James Howe opened DATAMONGO-1451 and commented

Currently all WriteConcernException are translated to DataIntegrityViolationException, a NonTransientDataAccessException.

However, a write timeout also throws a WriteConcernException, for example:

com.mongodb.WriteConcernException: { "serverUsed" : "10.10.17.35:27017" , "ok" : 1 , "n" : 0 , "wtimeout" : true , "err" : "waiting for replication timed out" , "code" : 64}

Logically (IMO) this case should instead be a TransientDataAccessException, perhaps a subclass of TransientDataAccessResourceException or QueryTimeoutException. At least create a MongoWriteTimeoutException so I can special-case it in exception handlers more easily.

Unfortunately, the WriteConcernException doesn't appear to provide direct access to the wtimeout property, so I guess you'd have to use ex.getResponse().

My end-goal: I want to return 503 from my controller when it times out, and 500 for other non-transient errors


Affects: 1.8.4 (Gosling SR4), 1.9.1 (Hopper SR1)

Referenced from: pull request https://github.com/spring-projects/spring-data-mongodb/pull/370

1 votes, 2 watchers

spring-projects-issues commented 8 years ago

James Howe commented

This will probably require version 3.x of mongo-java-driver

spring-projects-issues commented 8 years ago

Mark Paluch commented

The difficulty is staying compatible with both driver versions, 2.x and 3.x

spring-projects-issues commented 7 years ago

James Howe commented

I'm also having similar issues with "Write failed with error code 100 and error message 'Not enough data-bearing nodes'". Logically this would be the same as e.g. CassandraInsufficientReplicasAvailableException, another transient issue

christophstrobl commented 1 year ago

I don't think parsing the error message is the way to go here. The MongoDB error_codes.yml holds a - RetriableError category for against which we could compare the actual MongoException.getCode() value.