diff --git a/src/main/java/org/example/database/exception/DatabaseException.java b/src/main/java/org/example/database/exception/DatabaseException.java new file mode 100644 index 0000000..7605983 --- /dev/null +++ b/src/main/java/org/example/database/exception/DatabaseException.java @@ -0,0 +1,8 @@ +package org.example.database.exception; + +public class DatabaseException extends OrmException { + + public DatabaseException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/org/example/database/exception/DuplicateKeyException.java b/src/main/java/org/example/database/exception/DuplicateKeyException.java new file mode 100644 index 0000000..12c6282 --- /dev/null +++ b/src/main/java/org/example/database/exception/DuplicateKeyException.java @@ -0,0 +1,8 @@ +package org.example.database.exception; + +public class DuplicateKeyException extends OrmException { + + public DuplicateKeyException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/org/example/database/exception/EntityNotFoundException.java b/src/main/java/org/example/database/exception/EntityNotFoundException.java new file mode 100644 index 0000000..2a85c59 --- /dev/null +++ b/src/main/java/org/example/database/exception/EntityNotFoundException.java @@ -0,0 +1,8 @@ +package org.example.database.exception; + +public class EntityNotFoundException extends OrmException { + + public EntityNotFoundException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/org/example/database/exception/EntityValidationException.java b/src/main/java/org/example/database/exception/EntityValidationException.java new file mode 100644 index 0000000..60024e9 --- /dev/null +++ b/src/main/java/org/example/database/exception/EntityValidationException.java @@ -0,0 +1,8 @@ +package org.example.database.exception; + +public class EntityValidationException extends OrmException { + + public EntityValidationException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/org/example/database/exception/ExceptionTranslator.java b/src/main/java/org/example/database/exception/ExceptionTranslator.java new file mode 100644 index 0000000..f54e2b2 --- /dev/null +++ b/src/main/java/org/example/database/exception/ExceptionTranslator.java @@ -0,0 +1,43 @@ +package org.example.database.exception; + +import java.sql.SQLException; + +public class ExceptionTranslator { + + private static final String UNIQUE_VIOLATION_CODE = "23505"; + + public static OrmException translate(SQLException e, Class entityClass, String sql) { + String sqlState = e.getSQLState(); + + if (UNIQUE_VIOLATION_CODE.equals(sqlState)) { + String constraintName = extractConstraintName(e); + String message = String.format("Duplicate key violation on entity '%s': unique constraint '%s'", + entityClass != null ? entityClass.getSimpleName() : "unknown", + constraintName); + return new DuplicateKeyException(message); + } + + if (sqlState != null && sqlState.startsWith("23")) { + String message = String.format("Database error executing query '%s': %s", sql, e.getMessage()); + return new DatabaseException(message); + } + + String message = String.format("Database error executing query '%s': %s", sql, e.getMessage()); + return new DatabaseException(message); + } + + private static String extractConstraintName(SQLException e) { + String message = e.getMessage(); + if (message != null && message.contains("unique constraint")) { + int start = message.lastIndexOf("unique constraint \""); + if (start != -1) { + start += "unique constraint \"".length(); + int end = message.indexOf("\"", start); + if (end > start) { + return message.substring(start, end); + } + } + } + return "unknown"; + } +} \ No newline at end of file diff --git a/src/main/java/org/example/database/exception/OrmException.java b/src/main/java/org/example/database/exception/OrmException.java new file mode 100644 index 0000000..0c43ac4 --- /dev/null +++ b/src/main/java/org/example/database/exception/OrmException.java @@ -0,0 +1,8 @@ +package org.example.database.exception; + +public class OrmException extends RuntimeException { + + public OrmException(String message) { + super(message); + } +} \ No newline at end of file