Search in sources :

Example 1 with KapuaDuplicateNameException

use of org.eclipse.kapua.KapuaDuplicateNameException in project kapua by eclipse.

the class KapuaExceptionUtils method convertPersistenceException.

/**
 * Converts a low-level PersistenceException/SQLException to a business-level KapuaException.
 *
 * @param he
 */
public static KapuaException convertPersistenceException(Exception he) {
    Exception e = he;
    // Handle the case of an incoming KapuaException
    if (he instanceof KapuaException && he.getCause() != null && he.getCause() instanceof PersistenceException) {
        e = (Exception) he.getCause();
    } else if (he instanceof KapuaException) {
        return (KapuaException) e;
    }
    // process the Persistence Exception
    KapuaException ee = null;
    if (e instanceof OptimisticLockException) {
        ee = new KapuaOptimisticLockingException(e);
    } else {
        Throwable t = e.getCause();
        if (t instanceof PersistenceException) {
            t = t.getCause();
        }
        if (t instanceof RollbackException) {
            t = t.getCause();
        }
        if (t instanceof DatabaseException) {
            // Handle Unique Constraints Exception
            DatabaseException cve = (DatabaseException) t;
            int sqlErrorCode = cve.getErrorCode();
            switch(sqlErrorCode) {
                // SQL Error: 1062, SQLState: 23000 - ER_DUP_KEYNAME - Unique Constraints Exception
                case 1062:
                    {
                        // 
                        // Extract the constraint name
                        // e.g. SQL Message: Duplicate entry 'test_account_1,322,584,746,357' for key 'uc_accountName'
                        String message = cve.getInternalException().getMessage();
                        String[] parts = message.split("'");
                        String constraintName = parts[parts.length - 1];
                        // String duplicateNameField = s_uniqueConstraints.get(constraintName);
                        if (constraintName != null) {
                            ee = new KapuaDuplicateNameException(constraintName);
                        }
                    }
                    break;
                // SQL Error: 1048, SQLSTATE: 23000 - ER_BAD_NULL_ERROR - Not Null Violation
                case 1048:
                    {
                        // 
                        // Extract the name of the null attribute
                        // e.g. SQL Message: Column '%s' cannot be null
                        String message = cve.getInternalException().getMessage();
                        String[] parts = message.split("'");
                        String columnName = null;
                        if (parts.length == 3) {
                            columnName = parts[1];
                        }
                        // populate the null field name
                        if (columnName != null) {
                            ee = new KapuaIllegalNullArgumentException(columnName);
                        }
                    }
                    break;
            }
        }
    }
    // Handle all other Exceptions
    if (ee == null) {
        ee = KapuaException.internalError(e, "Error during Persistence Operation");
    }
    return ee;
}
Also used : KapuaIllegalNullArgumentException(org.eclipse.kapua.KapuaIllegalNullArgumentException) PersistenceException(javax.persistence.PersistenceException) KapuaDuplicateNameException(org.eclipse.kapua.KapuaDuplicateNameException) OptimisticLockException(javax.persistence.OptimisticLockException) KapuaException(org.eclipse.kapua.KapuaException) KapuaOptimisticLockingException(org.eclipse.kapua.KapuaOptimisticLockingException) RollbackException(javax.persistence.RollbackException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) KapuaOptimisticLockingException(org.eclipse.kapua.KapuaOptimisticLockingException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) OptimisticLockException(javax.persistence.OptimisticLockException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) KapuaDuplicateNameException(org.eclipse.kapua.KapuaDuplicateNameException) KapuaException(org.eclipse.kapua.KapuaException) KapuaIllegalNullArgumentException(org.eclipse.kapua.KapuaIllegalNullArgumentException)

Aggregations

OptimisticLockException (javax.persistence.OptimisticLockException)1 PersistenceException (javax.persistence.PersistenceException)1 RollbackException (javax.persistence.RollbackException)1 KapuaDuplicateNameException (org.eclipse.kapua.KapuaDuplicateNameException)1 KapuaException (org.eclipse.kapua.KapuaException)1 KapuaIllegalNullArgumentException (org.eclipse.kapua.KapuaIllegalNullArgumentException)1 KapuaOptimisticLockingException (org.eclipse.kapua.KapuaOptimisticLockingException)1 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)1