public interface ExceptionMapper
ExceptionMapper interface allows users to plug in specific exception mapping functionality.
When ObjectGrid gets a generic Throwable from a third-party product, this interface can be
used to map it to a different Throwable which can be more friendly or consumable to ObjectGrid.
For example, when a JPALoader or a JPAEntityLoader is used, the application could implement
this interface to map a generic javax.persistence.PersistenceException to more consumable exceptions.
The implementation class could introspect the SQL state and error code of the java.sql.SQLException chained in the
JPA exception and throw a LoaderNotAvailableException if the SQL state or error code indicates
the database server or network is not functional or the database runs out of resources.
Here is an example:
public Throwable map(Throwable original) {
Throwable cause = original;
while (cause != null) {
// keep looping to check the next chained exception
if (cause instanceof SQLException) {
// Only check if the exception is an SQLException
SQLException sqle = (SQLException) cause;
// If the loader not available SQL state set contains this SQL state, then
// we return a LoaderNotAvailableException with the original exception chained in it.
if (loaderNotAvailableSQLStateSet.contains(sqle.getSQLState())) {
return new LoaderNotAvailableException(original);
}
// If the loader not available SQL error code set contains this error code, then
// we return a LoaderNotAvailableException with the original exception chained in it
if (loaderNotAvailableSQLErrorSet.contains(new Integer(sqle.getErrorCode()))) {
return new LoaderNotAvailableException(original);
}
}
// Get the next chained exception
Throwable newCause = cause.getCause();
// Safe-guard check to avoid indefinite loop if the exception chains itself
if (newCause == cause) {
// Always return the original exception if cannot map it.
return original;
} else {
cause = newCause;
}
}
// Always retrun the original exception if cannot map it.
return original;
}
Currently, the ExceptionMapper can be configured in the following ObjectGrid beans:
Throwable map(Throwable original)
Throwable to a more friendly or consumable Throwable if possible.
If the passed-in Throwable cannot be mapped, the implementation is expected to return it.original - the Throwable to be mappedThrowable , or the original Throwable if it cannot be mapped.