From ad80931b47bc245faa4c2440ecc9171a7d2a715d Mon Sep 17 00:00:00 2001 From: Sergejs Visockis Date: Fri, 29 Mar 2024 17:41:38 +0100 Subject: [PATCH] work on exceptions (#79) --- .../com/iluwatar/activerecord/RecordBase.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java b/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java index 1aa748c7c402..c1b315b4d7b1 100644 --- a/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java +++ b/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java @@ -19,14 +19,20 @@ @RequiredArgsConstructor public abstract class RecordBase> { + private static final String EXCEPTION_MESSAGE = "Couldn't execute database query for the following domain model :"; + @Setter private static DataSource dataSource; @SuppressWarnings({"unchecked"}) private final Class clazz = (Class) getClass(); - protected Connection getConnection() throws SQLException { - return dataSource.getConnection(); + protected Connection getConnection() { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + throw new RuntimeException("Unable to acquire database connection", e); + } } /** @@ -58,9 +64,7 @@ public List findAll() { return recordList; } } catch (SQLException e) { - throw new RuntimeException( - "Unable to find all the records for the following domain model : " + clazz.getName() - + " due to the data persistence error", e); + throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName(), e); } } @@ -83,10 +87,7 @@ public T findById(Long id) { return getDeclaredClassInstance(); } } catch (SQLException e) { - throw new RuntimeException( - "Unable to find a record for the following domain model : " + clazz.getName() + " by id=" - + id - + " due to the data persistence error", e); + throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName() + " with id=" + id, e); } } @@ -103,9 +104,7 @@ public void save() { pstmt.executeUpdate(); } catch (SQLException e) { - throw new RuntimeException( - "Unable to save the record for the following domain model : " + clazz.getName() - + " due to the data persistence error", e); + throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName(), e); } }