1 package com.imcode.db; 2 3 import java.sql.SQLException; 4 5 import com.imcode.db.exceptions.IntegrityConstraintViolationException; 6 import com.imcode.db.exceptions.StringTruncationException; 7 8 public class DatabaseException extends RuntimeException { 9 10 public DatabaseException( String message, Throwable ex ) { 11 super( message, ex ) ; 12 } 13 14 public static DatabaseException fromSQLException( String message, SQLException cause ) { 15 DatabaseException result ; 16 String sqlState = cause.getSQLState(); 17 if ( "23000".equals( sqlState ) ) { 18 result = new IntegrityConstraintViolationException( message, cause ); 19 } else if ( "01004".equals( sqlState ) ) { 20 result = new StringTruncationException( message, cause ); 21 } else { 22 result = new DatabaseException( "SQLException with SQLState " + sqlState+": "+message, cause ) ; 23 } 24 result.setStackTrace( cause.getStackTrace() ); 25 return result ; 26 } 27 }