0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.