RESOLVED - issue BATCH-188: LogOrRethrowException handler swallows unclassified exceptions

http://opensource.atlassian.com/projects/spring/browse/BATCH-188
This commit is contained in:
dsyer
2007-11-02 11:01:02 +00:00
parent 737e95c888
commit ef608de7ae
2 changed files with 35 additions and 12 deletions

View File

@@ -62,7 +62,8 @@ public class LogOrRethrowExceptionHandler implements ExceptionHandler {
*/
public static final String ERROR = "error";
protected final Log logger = LogFactory.getLog(LogOrRethrowExceptionHandler.class);
protected final Log logger = LogFactory
.getLog(LogOrRethrowExceptionHandler.class);
private ExceptionClassifier exceptionClassifier = new ExceptionClassifierSupport() {
public Object classify(Throwable throwable) {
@@ -83,26 +84,33 @@ public class LogOrRethrowExceptionHandler implements ExceptionHandler {
/**
* Classify the throwables and decide whether to rethrow based on the
* result. The context is not used.
* @throws Exception
*
* @throws Exception
*
* @see {@link ExceptionHandler#handleExceptions(RepeatContext, Collection)}
*/
public void handleExceptions(RepeatContext context, Collection throwables) throws RuntimeException {
public void handleExceptions(RepeatContext context, Collection throwables)
throws RuntimeException {
for (Iterator iter = throwables.iterator(); iter.hasNext();) {
Throwable throwable = (Throwable) iter.next();
Object key = exceptionClassifier.classify(throwable);
if (ERROR.equals(key)) {
logger.error("Exception encountered in batch repeat.", throwable);
}
if (WARN.equals(key)) {
logger.warn("Exception encountered in batch repeat.", throwable);
}
if (DEBUG.equals(key) && logger.isDebugEnabled()) {
logger.debug("Exception encountered in batch repeat.", throwable);
}
if (RETHROW.equals(key)) {
logger.error("Exception encountered in batch repeat.",
throwable);
} else if (WARN.equals(key)) {
logger
.warn("Exception encountered in batch repeat.",
throwable);
} else if (DEBUG.equals(key) && logger.isDebugEnabled()) {
logger.debug("Exception encountered in batch repeat.",
throwable);
} else if (RETHROW.equals(key)) {
DefaultExceptionHandler.rethrow(throwable);
} else {
throw new IllegalStateException(
"Unclassified exception encountered. Did you mean to classifiy this as 'rethrow'?",
throwable);
}
}

View File

@@ -96,4 +96,19 @@ public class LogOrRethrowExceptionHandlerTests extends TestCase {
assertNotNull(writer.toString());
}
public void testUnclassifiedException() throws Exception {
handler.setExceptionClassifier(new ExceptionClassifierSupport() {
public Object classify(Throwable throwable) {
return "DEFAULT";
}
});
try {
handler.handleExceptions(context, Collections.singleton(new Error("Foo")));
fail("Expected IllegalStateException");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().toLowerCase().indexOf("unclassified")>=0);
assertEquals("Foo", e.getCause().getMessage());
}
}
}