문제

Can anyone know best practice translate specific mongodb error code to own exception code. Spring data mongodb have got any converters or mechanism for that ?

in my case i need throw my own error when _id field is duplicate. So for example now my error is :

org.springframework.dao.DuplicateKeyException: { "serverUsed" : "/127.0.0.1:27017" , "err" : "E11000 duplicate key error index: test.member.$_id_  dup key: { : \"lukasz\" }" , "code" : 11000 , "n" : 0 , "connectionId" : 35 , "ok" : 1.0}; nested exception is com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "/127.0.0.1:27017" , "err" : "E11000 duplicate key error index: test.member.$_id_  dup key: { : \"lukasz\" }" , "code" : 11000 , "n" : 0 , "connectionId" : 35 , "ok" : 1.0}

and i need throw UsernameAlreadyExist ...

i need to mention that i need translate that error only for one collection

thanks for help

도움이 되었습니까?

해결책

A reasonable approach would be to simply catch DuplicateKeyException in an appropriate place and throw your UsernameAlreadyExist exception (potentially wrapping the source one).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top