Skip to content

Commit

Permalink
[#7040] Add DataAccessException.getCause(Class<? extends Throwable>)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Jan 12, 2018
1 parent 23afe48 commit 3fe26d6
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions jOOQ/src/main/java/org/jooq/exception/DataAccessException.java
Expand Up @@ -53,7 +53,12 @@ public class DataAccessException extends RuntimeException {
/** /**
* Generated UID * Generated UID
*/ */
private static final long serialVersionUID = 491834858363345767L; private static final long serialVersionUID = 491834858363345767L;

/**
* Never run infinite loops
*/
private static int maxCauseLookups = 256;


/** /**
* Constructor for DataAccessException. * Constructor for DataAccessException.
Expand Down Expand Up @@ -81,9 +86,9 @@ public DataAccessException(String message, Throwable cause) {
* {@link SQLException}. * {@link SQLException}.
*/ */
public String sqlState() { public String sqlState() {
Throwable t = getCause(); SQLException e = getCause(SQLException.class);
if (t instanceof SQLException) if (e != null)
return ((SQLException) t).getSQLState(); return e.getSQLState();


return "00000"; return "00000";
} }
Expand All @@ -94,9 +99,9 @@ public String sqlState() {
* caused by a {@link SQLException}. * caused by a {@link SQLException}.
*/ */
public SQLStateClass sqlStateClass() { public SQLStateClass sqlStateClass() {
Throwable t = getCause(); SQLException e = getCause(SQLException.class);
if (t instanceof SQLException) if (e != null)
return SQLStateClass.fromCode(((SQLException) t).getSQLState()); return SQLStateClass.fromCode(e.getSQLState());


return SQLStateClass.NONE; return SQLStateClass.NONE;
} }
Expand All @@ -107,9 +112,9 @@ public SQLStateClass sqlStateClass() {
* caused by a {@link SQLException}. * caused by a {@link SQLException}.
*/ */
public SQLStateSubclass sqlStateSubclass() { public SQLStateSubclass sqlStateSubclass() {
Throwable t= getCause(); SQLException e = getCause(SQLException.class);
if (t instanceof SQLException) if (e != null)
return SQLStateSubclass.fromCode(((SQLException) t).getSQLState()); return SQLStateSubclass.fromCode(e.getSQLState());


return SQLStateSubclass.NONE; return SQLStateSubclass.NONE;
} }
Expand All @@ -119,5 +124,30 @@ public StackTraceElement[] getStackTrace() {
return super.getStackTrace(); return super.getStackTrace();
} }


/**
* Find a root cause of a given type, or <code>null</code> if no root cause
* of that type was found.
*/
@SuppressWarnings("unchecked")
public <T extends Throwable> T getCause(Class<? extends T> type) {
Throwable next = getCause();
Throwable prev;

for (int i = 0; i < maxCauseLookups; i++) {
if (next == null)
return null;

if (type.isInstance(next))
return (T) next;

prev = next;
next = next.getCause();


// Don't trust exceptions to respect the default behaviour of Throwable.getCause()
if (prev == next)
return null;
}

return null;
}
} }

0 comments on commit 3fe26d6

Please sign in to comment.