Skip to content

Commit ad80931

Browse files
work on exceptions (iluwatar#79)
1 parent 99d5a5a commit ad80931

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@
1919
@RequiredArgsConstructor
2020
public abstract class RecordBase<T extends RecordBase<?>> {
2121

22+
private static final String EXCEPTION_MESSAGE = "Couldn't execute database query for the following domain model :";
23+
2224
@Setter
2325
private static DataSource dataSource;
2426

2527
@SuppressWarnings({"unchecked"})
2628
private final Class<T> clazz = (Class<T>) getClass();
2729

28-
protected Connection getConnection() throws SQLException {
29-
return dataSource.getConnection();
30+
protected Connection getConnection() {
31+
try {
32+
return dataSource.getConnection();
33+
} catch (SQLException e) {
34+
throw new RuntimeException("Unable to acquire database connection", e);
35+
}
3036
}
3137

3238
/**
@@ -58,9 +64,7 @@ public List<T> findAll() {
5864
return recordList;
5965
}
6066
} catch (SQLException e) {
61-
throw new RuntimeException(
62-
"Unable to find all the records for the following domain model : " + clazz.getName()
63-
+ " due to the data persistence error", e);
67+
throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName(), e);
6468
}
6569
}
6670

@@ -83,10 +87,7 @@ public T findById(Long id) {
8387
return getDeclaredClassInstance();
8488
}
8589
} catch (SQLException e) {
86-
throw new RuntimeException(
87-
"Unable to find a record for the following domain model : " + clazz.getName() + " by id="
88-
+ id
89-
+ " due to the data persistence error", e);
90+
throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName() + " with id=" + id, e);
9091
}
9192
}
9293

@@ -103,9 +104,7 @@ public void save() {
103104
pstmt.executeUpdate();
104105

105106
} catch (SQLException e) {
106-
throw new RuntimeException(
107-
"Unable to save the record for the following domain model : " + clazz.getName()
108-
+ " due to the data persistence error", e);
107+
throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName(), e);
109108
}
110109
}
111110

0 commit comments

Comments
 (0)