File tree Expand file tree Collapse file tree
PgBulkInsert/pgbulkinsert-core/src
main/java/de/bytefish/pgbulkinsert/util
test/java/de/bytefish/pgbulkinsert/test/util Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
2+
3+ package de .bytefish .pgbulkinsert .util ;
4+
5+ import org .checkerframework .checker .nullness .qual .Nullable ;
6+
7+ public class ExceptionUtils {
8+
9+ private ExceptionUtils () {}
10+
11+ @ Nullable
12+ public static Throwable getRootCause (@ Nullable Throwable t ) {
13+ if (t == null ) {
14+ return null ;
15+ }
16+
17+ Throwable rootCause = null ;
18+ Throwable cause = t .getCause ();
19+
20+ // Now get to the Inner-Most Cause:
21+ while (cause != null && cause != rootCause ) {
22+ rootCause = cause ;
23+ cause = cause .getCause ();
24+ }
25+
26+ return rootCause ;
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
2+
3+ package de .bytefish .pgbulkinsert .test .util ;
4+
5+ import de .bytefish .pgbulkinsert .exceptions .BinaryWriteFailedException ;
6+ import de .bytefish .pgbulkinsert .util .ExceptionUtils ;
7+ import org .junit .Assert ;
8+ import org .junit .Test ;
9+
10+ import java .io .IOException ;
11+ import java .sql .SQLException ;
12+
13+ public class ExceptionUtilsTest {
14+
15+ @ Test
16+ public void testExceptionUtils () {
17+
18+ // Exception Hierarchy:
19+ SQLException sqlException = new SQLException ("My SQLException" );
20+ IOException ioException = new IOException ("My IOException with a SQLException cause" , sqlException );
21+ BinaryWriteFailedException binaryWriteFailedException = new BinaryWriteFailedException (ioException );
22+
23+ // Get the root cause:
24+ Throwable innerMostException = ExceptionUtils .getRootCause (binaryWriteFailedException );
25+
26+ Assert .assertNotNull (innerMostException );
27+ Assert .assertEquals ("My SQLException" , innerMostException .getMessage ());
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments