Pregunta

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

¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top