remove lastException in task.variables

This commit is contained in:
huanghongxun 2019-02-20 20:07:43 +08:00
parent 3ae826d9ca
commit e57180eb15
13 changed files with 22 additions and 27 deletions

View File

@ -106,7 +106,7 @@ public class AddAuthlibInjectorServerPane extends StackPane implements DialogAwa
Task.of(() -> {
serverBeingAdded = AuthlibInjectorServer.locateServer(url);
}).finalized(Schedulers.javafx(), (variables, isDependentsSucceeded) -> {
}).finalized(Schedulers.javafx(), (variables, isDependentsSucceeded, exception) -> {
addServerPane.setDisable(false);
nextPane.hideSpinner();
@ -118,7 +118,6 @@ public class AddAuthlibInjectorServerPane extends StackPane implements DialogAwa
transitionHandler.setContent(confirmServerPane, ContainerAnimations.SWIPE_LEFT.getAnimationProducer());
} else {
Exception exception = variables.get("lastException");
LOG.log(Level.WARNING, "Failed to resolve auth server: " + url, exception);
lblCreationWarning.setText(resolveFetchExceptionMessage(exception));
}

View File

@ -59,7 +59,7 @@ public final class VanillaInstallWizardProvider implements WizardProvider {
if (settings.containsKey("optifine"))
builder.version((RemoteVersion) settings.get("optifine"));
return builder.buildAsync().finalized((a, b) -> profile.getRepository().refreshVersions())
return builder.buildAsync().finalized((a, b, c) -> profile.getRepository().refreshVersions())
.then(Task.of(Schedulers.javafx(), () -> profile.setSelectedVersion(name)));
}

View File

@ -128,7 +128,7 @@ public final class VersionsPage extends BorderPane implements WizardPage, Refres
@Override
public void refresh() {
transitionHandler.setContent(spinner, ContainerAnimations.FADE.getAnimationProducer());
executor = versionList.refreshAsync(gameVersion, downloadProvider).finalized((variables, isDependentsSucceeded) -> {
executor = versionList.refreshAsync(gameVersion, downloadProvider).finalized((variables, isDependentsSucceeded, exception) -> {
if (isDependentsSucceeded) {
List<VersionsPageItem> items = loadVersions();
@ -147,7 +147,7 @@ public final class VersionsPage extends BorderPane implements WizardPage, Refres
}
});
} else {
LOG.log(Level.WARNING, "Failed to fetch versions list", (Throwable) variables.get("lastException"));
LOG.log(Level.WARNING, "Failed to fetch versions list", exception);
Platform.runLater(() -> {
transitionHandler.setContent(failedPane, ContainerAnimations.FADE.getAnimationProducer());
});

View File

@ -92,7 +92,7 @@ public final class ModListPage extends Control {
modManager.refreshMods();
return new LinkedList<>(modManager.getMods());
}
}).finalizedResult(Schedulers.javafx(), (list, isDependentsSucceeded) -> {
}).finalizedResult(Schedulers.javafx(), (list, isDependentsSucceeded, exception) -> {
loadingProperty().set(false);
if (isDependentsSucceeded)
FXUtils.onWeakChangeAndOperate(parentTab.getSelectionModel().selectedItemProperty(), newValue -> {

View File

@ -52,7 +52,7 @@ public class WorldListPage extends ListPage<WorldListItem> {
setLoading(true);
Task.ofResult(() -> World.getWorlds(savesDir).parallel().collect(Collectors.toList()))
.finalizedResult(Schedulers.javafx(), (result, isDependentsSucceeded) -> {
.finalizedResult(Schedulers.javafx(), (result, isDependentsSucceeded, exception) -> {
setLoading(false);
if (isDependentsSucceeded)
itemsProperty().setAll(result.stream().map(WorldListItem::new).collect(Collectors.toList()));

View File

@ -121,8 +121,7 @@ public final class UpdateHandler {
}
} else {
Throwable e = task.getLastException();
LOG.log(Level.WARNING, "Failed to update to " + version, e);
LOG.log(Level.WARNING, "Failed to update to " + version, executor.getLastException());
Platform.runLater(() -> Controllers.dialog(e.toString(), i18n("update.failed"), MessageBox.ERROR_MESSAGE));
}
});

View File

@ -69,7 +69,7 @@ public class DefaultGameBuilder extends GameBuilder {
result = result.then(dependencyManager.installLibraryAsync(remoteVersion));
return result;
}).finalized((variables, isDependentsSucceeded) -> {
}).finalized((variables, isDependentsSucceeded, exception) -> {
if (!isDependentsSucceeded)
dependencyManager.getGameRepository().getVersionRoot(name).delete();
});

View File

@ -20,5 +20,5 @@ package org.jackhuang.hmcl.task;
import org.jackhuang.hmcl.util.AutoTypingMap;
public interface FinalizedCallback {
void execute(AutoTypingMap<String> variables, boolean isDependentsSucceeded) throws Exception;
void execute(AutoTypingMap<String> variables, boolean isDependentsSucceeded, Exception exception) throws Exception;
}

View File

@ -27,7 +27,7 @@ import java.util.Collections;
*/
final class FinalizedTask extends Task {
private final Collection<Task> dependents;
private final Task pred;
private final FinalizedCallback callback;
private final Scheduler scheduler;
@ -38,7 +38,7 @@ final class FinalizedTask extends Task {
* @param callback a callback that returns the task runs after pred, succ will be executed asynchronously. You can do something that relies on the result of pred.
*/
public FinalizedTask(Task pred, Scheduler scheduler, FinalizedCallback callback, String name) {
this.dependents = Collections.singleton(pred);
this.pred = pred;
this.scheduler = scheduler;
this.callback = callback;
@ -53,7 +53,7 @@ final class FinalizedTask extends Task {
@Override
public void execute() throws Exception {
callback.execute(getVariables(), isDependentsSucceeded());
callback.execute(getVariables(), isDependentsSucceeded(), pred.getLastException());
if (!isDependentsSucceeded())
throw new SilentException();
@ -61,7 +61,7 @@ final class FinalizedTask extends Task {
@Override
public Collection<Task> getDependents() {
return dependents;
return Collections.singleton(pred);
}
@Override

View File

@ -68,13 +68,13 @@ public abstract class Task {
this.state = state;
}
private Throwable lastException = null;
private Exception lastException;
public Throwable getLastException() {
public Exception getLastException() {
return lastException;
}
void setLastException(Throwable e) {
void setLastException(Exception e) {
lastException = e;
}
@ -321,7 +321,7 @@ public abstract class Task {
}
public final <T extends Exception, K extends Exception> Task finalized(Scheduler scheduler, ExceptionalConsumer<AutoTypingMap<String>, T> success, ExceptionalConsumer<Exception, K> failure) {
return finalized(scheduler, (variables, isDependentsSucceeded) -> {
return finalized(scheduler, (variables, isDependentsSucceeded, exception) -> {
if (isDependentsSucceeded) {
if (success != null)
try {
@ -331,10 +331,9 @@ public abstract class Task {
if (failure != null)
failure.accept(e);
}
}
else {
} else {
if (failure != null)
failure.accept(variables.get(TaskExecutor.LAST_EXCEPTION_ID));
failure.accept(exception);
}
});
}

View File

@ -234,7 +234,6 @@ public final class TaskExecutor {
} catch (Exception e) {
task.setLastException(e);
lastException = e;
variables.set(LAST_EXCEPTION_ID, e);
if (task.getSignificance().shouldLog()) {
Logging.LOG.log(Level.FINE, "Task failed: " + task.getName(), e);
}
@ -279,6 +278,4 @@ public final class TaskExecutor {
}
}
public static final String LAST_EXCEPTION_ID = "lastException";
}

View File

@ -61,7 +61,7 @@ public abstract class TaskResult<V> extends Task {
public Task finalizedResult(Scheduler scheduler, FinalizedCallback<V> callback) {
return new FinalizedTask(this, scheduler,
(variables, isDependentsSucceeded) -> callback.execute(getResult(), isDependentsSucceeded),
(variables, isDependentsSucceeded, exception) -> callback.execute(getResult(), isDependentsSucceeded, exception),
ReflectionHelper.getCaller().toString());
}
@ -98,6 +98,6 @@ public abstract class TaskResult<V> extends Task {
}
public interface FinalizedCallback<V> {
void execute(V result, boolean isDependentsSucceeded) throws Exception;
void execute(V result, boolean isDependentsSucceeded, Exception exception) throws Exception;
}
}

View File

@ -35,6 +35,7 @@ public interface Validation {
* Throw an exception when values are malformed.
*
* @throws JsonParseException if fields are filled in wrong format or wrong type.
* @throws TolerableValidationException if we want to replace this object with null (i.e. the object does not fulfill the constraints).
*/
void validate() throws JsonParseException, TolerableValidationException;
}