Handling Unique Constraint Violation Exceptions

Jmix framework allows you to customize the message displayed by an exception handler for a database constraint violation error.

A custom message should be specified in the message bundle. Message key should have a databaseUniqueConstraintViolation prefix plus the database unique index name in uppercase. For example:

databaseUniqueConstraintViolation.UC_UIEX1_CUSTOMER_EMAIL=A customer with \
  the same email already exists

So, the default notification looks like this:

with default exception message

After you add a custom message key, you will get the following notification:

with custom exception message

Detection of database constraint violation errors is done by the UniqueConstraintViolationHandler class which determines the exception type by searching a special marker string in the messages of all exceptions in the chain.

You can also completely replace the standard handler by providing your own exception handler with a higher precedence, for example, @Order(JmixOrder.HIGHEST_PRECEDENCE - 10).