fix: broken error handling

This commit is contained in:
huanghongxun
2020-02-06 12:47:25 +08:00
parent 0d906e0adf
commit bdb6784a9b

View File

@@ -135,10 +135,7 @@ public class CancellableTaskExecutor extends TaskExecutor {
try { try {
Schedulers.schedule(task.getExecutor(), wrap(task::preExecute)).get(); Schedulers.schedule(task.getExecutor(), wrap(task::preExecute)).get();
} catch (ExecutionException e) { } catch (ExecutionException e) {
if (e.getCause() instanceof Exception) rethrow(e);
throw (Exception) e.getCause();
else
throw e;
} }
} }
@@ -160,10 +157,7 @@ public class CancellableTaskExecutor extends TaskExecutor {
task.execute(); task.execute();
})).get(); })).get();
} catch (ExecutionException e) { } catch (ExecutionException e) {
if (e.getCause() instanceof Exception) rethrow(e);
throw (Exception) e.getCause();
else
throw e;
} finally { } finally {
task.setState(Task.TaskState.EXECUTED); task.setState(Task.TaskState.EXECUTED);
} }
@@ -179,10 +173,7 @@ public class CancellableTaskExecutor extends TaskExecutor {
try { try {
Schedulers.schedule(task.getExecutor(), wrap(task::postExecute)).get(); Schedulers.schedule(task.getExecutor(), wrap(task::postExecute)).get();
} catch (ExecutionException e) { } catch (ExecutionException e) {
if (e.getCause() instanceof Exception) rethrow(e);
throw (Exception) e.getCause();
else
throw e;
} }
} }
@@ -199,29 +190,42 @@ public class CancellableTaskExecutor extends TaskExecutor {
task.onDone().fireEvent(new TaskEvent(this, task, false)); task.onDone().fireEvent(new TaskEvent(this, task, false));
taskListeners.forEach(it -> it.onFinished(task)); taskListeners.forEach(it -> it.onFinished(task));
} catch (InterruptedException e) { } catch (RejectedExecutionException ignored) {
task.setException(e); } catch (Exception throwable) {
if (task.getSignificance().shouldLog()) { Throwable resolved = resolveException(throwable);
Logging.LOG.log(Level.FINE, "Task aborted: " + task.getName()); if (resolved instanceof Exception) {
Exception e = (Exception) resolved;
if (e instanceof InterruptedException || e instanceof CancellationException) {
task.setException(e);
if (task.getSignificance().shouldLog()) {
Logging.LOG.log(Level.FINE, "Task aborted: " + task.getName());
}
task.onDone().fireEvent(new TaskEvent(this, task, true));
taskListeners.forEach(it -> it.onFailed(task, e));
} else {
task.setException(e);
exception = e;
if (task.getSignificance().shouldLog()) {
Logging.LOG.log(Level.FINE, "Task failed: " + task.getName(), e);
}
task.onDone().fireEvent(new TaskEvent(this, task, true));
taskListeners.forEach(it -> it.onFailed(task, e));
}
} else if (resolved instanceof Error) {
throw (Error) resolved;
} }
task.onDone().fireEvent(new TaskEvent(this, task, true));
taskListeners.forEach(it -> it.onFailed(task, e));
} catch (CancellationException | RejectedExecutionException e) {
if (task.getException() == null)
task.setException(e);
} catch (Exception e) {
task.setException(e);
exception = e;
if (task.getSignificance().shouldLog()) {
Logging.LOG.log(Level.FINE, "Task failed: " + task.getName(), e);
}
task.onDone().fireEvent(new TaskEvent(this, task, true));
taskListeners.forEach(it -> it.onFailed(task, e));
} }
task.setState(flag ? Task.TaskState.SUCCEEDED : Task.TaskState.FAILED); task.setState(flag ? Task.TaskState.SUCCEEDED : Task.TaskState.FAILED);
return flag; return flag;
} }
private static Throwable resolveException(Throwable e) {
if (e instanceof ExecutionException || e instanceof CompletionException)
return resolveException(e.getCause());
else
return e;
}
private static void rethrow(Throwable e) { private static void rethrow(Throwable e) {
if (e == null) if (e == null)
return; return;