Unique Index Custom Exception Handler

Dear CubaTeam,
I have an entity with unique index (See attached img). How to create custom exception handler ? so i can return custom message in case of duplicate key entry. I have tried this CUBA Platform. Developer’s Manual but it’s not working.

error message

unique index

In fact, the standard UniqueConstraintViolationHandler allows you to customize the message for your constraint very easily: just provide a message in the main message pack of your web module with the key equal to the constraint name, for example:

IDX_SOMETHING_UNQ = Something with the same name already exists

If UniqueConstraintViolationHandler cannot determine the unique constraint violation correctly, look at this application property: cuba.uniqueConstraintViolationPattern. It allows you to set up a pattern different from the default used for your database.

1 Like


I have added
in message.properties of web, portal and core modules but not getting this message.

In the log i am getting
"Caused by: org.eclipse.persistence.exceptions.DatabaseException:
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicata du champ ‘9538578696’ pour la clef 'IDX_APPOLL_USER_DETAILS_UNIQ_PHONE_NUMBER’
Error Code: 1062"

and in the postman I am getting
“error”: “Server error”,
“details”: “”

these messages.

A post was split to a new topic: Unique index constraint message with multiple languages

I have added this in message.properties in web module but still the constraint violation message remains the same. @knstvk

Thanks and Regards

Probabily the class handler UniqueConstraintViolationHandler aren’t not matching the pattern defined in the aplication property (‘cuba.uniqueConstraintViolationPattern’);
Maybe the Throwable message are in another language and don’t will match the pattern;