Remove SilentException

This commit is contained in:
huanghongxun 2019-02-24 11:35:52 +08:00
parent ed66b016db
commit b22e724a3f
6 changed files with 32 additions and 49 deletions

View File

@ -20,17 +20,18 @@ package org.jackhuang.hmcl.ui;
import com.jfoenix.utils.JFXUtilities; import com.jfoenix.utils.JFXUtilities;
import org.jackhuang.hmcl.auth.Account; import org.jackhuang.hmcl.auth.Account;
import org.jackhuang.hmcl.auth.AuthInfo; import org.jackhuang.hmcl.auth.AuthInfo;
import org.jackhuang.hmcl.auth.AuthenticationException;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilAccount;
import org.jackhuang.hmcl.task.SilentException;
import org.jackhuang.hmcl.ui.account.AccountLoginPane; import org.jackhuang.hmcl.ui.account.AccountLoginPane;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
public final class DialogController { public final class DialogController {
public static AuthInfo logIn(Account account) throws Exception { public static AuthInfo logIn(Account account) throws CancellationException, AuthenticationException, InterruptedException {
if (account instanceof YggdrasilAccount) { if (account instanceof YggdrasilAccount) {
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
AtomicReference<AuthInfo> res = new AtomicReference<>(null); AtomicReference<AuthInfo> res = new AtomicReference<>(null);
@ -42,7 +43,7 @@ public final class DialogController {
Controllers.dialog(pane); Controllers.dialog(pane);
}); });
latch.await(); latch.await();
return Optional.ofNullable(res.get()).orElseThrow(SilentException::new); return Optional.ofNullable(res.get()).orElseThrow(CancellationException::new);
} }
return account.logIn(); return account.logIn();
} }

View File

@ -37,6 +37,7 @@ import static org.jackhuang.hmcl.util.Lang.thread;
import static org.jackhuang.hmcl.util.Logging.LOG; import static org.jackhuang.hmcl.util.Logging.LOG;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n; import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
import java.util.concurrent.CancellationException;
import java.util.logging.Level; import java.util.logging.Level;
public class AccountListItem extends RadioButton { public class AccountListItem extends RadioButton {
@ -84,6 +85,8 @@ public class AccountListItem extends RadioButton {
} catch (CredentialExpiredException e) { } catch (CredentialExpiredException e) {
try { try {
DialogController.logIn(account); DialogController.logIn(account);
} catch (CancellationException e1) {
// ignore cancellation
} catch (Exception e1) { } catch (Exception e1) {
LOG.log(Level.WARNING, "Failed to refresh " + account + " with password", e1); LOG.log(Level.WARNING, "Failed to refresh " + account + " with password", e1);
} }

View File

@ -18,7 +18,6 @@
package org.jackhuang.hmcl.mod; package org.jackhuang.hmcl.mod;
import org.jackhuang.hmcl.game.DefaultGameRepository; import org.jackhuang.hmcl.game.DefaultGameRepository;
import org.jackhuang.hmcl.task.SilentException;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
@ -49,11 +48,6 @@ public class ModpackUpdateTask extends Task {
} }
} }
@Override
public boolean isRelyingOnDependencies() {
return false;
}
@Override @Override
public Collection<? extends Task> getDependencies() { public Collection<? extends Task> getDependencies() {
return Collections.singleton(updateTask); return Collections.singleton(updateTask);
@ -78,8 +72,6 @@ public class ModpackUpdateTask extends Task {
repository.removeVersionFromDisk(id); repository.removeVersionFromDisk(id);
FileUtils.copyDirectory(backupFolder, repository.getVersionRoot(id).toPath()); FileUtils.copyDirectory(backupFolder, repository.getVersionRoot(id).toPath());
throw new SilentException();
} }
} }
} }

View File

@ -1,28 +0,0 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2019 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.task;
/**
* If a task throws {@link SilentException},
* the task will be marked as failure but do not log the stacktrace.
*
* @author huangyuhui
*/
public final class SilentException extends Exception {
}

View File

@ -33,6 +33,7 @@ import org.jackhuang.hmcl.util.function.ExceptionalSupplier;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.logging.Level; import java.util.logging.Level;
/** /**
@ -154,8 +155,15 @@ public abstract class Task {
} }
/** /**
* This method will be called after dependency tasks terminated all together.
*
* You can check whether dependencies succeed in this method by calling
* {@link Task#isDependenciesSucceeded()} no matter when
* {@link Task#isRelyingOnDependencies()} returns true or false.
*
* @throws InterruptedException if current thread is interrupted * @throws InterruptedException if current thread is interrupted
* @see Thread#isInterrupted * @see Thread#isInterrupted
* @see Task#isDependenciesSucceeded()
*/ */
public void postExecute() throws Exception {} public void postExecute() throws Exception {}
@ -370,8 +378,13 @@ public abstract class Task {
public void execute() throws Exception { public void execute() throws Exception {
action.execute(isDependentsSucceeded(), Task.this.getLastException()); action.execute(isDependentsSucceeded(), Task.this.getLastException());
if (!isDependentsSucceeded()) if (!isDependentsSucceeded()) {
throw new SilentException(); setSignificance(TaskSignificance.MINOR);
if (Task.this.getLastException() == null)
throw new CancellationException();
else
throw Task.this.getLastException();
}
} }
@Override @Override

View File

@ -163,7 +163,7 @@ public final class TaskExecutor {
Exception dependentsException = dependents.stream().map(Task::getLastException).filter(Objects::nonNull).findAny().orElse(null); Exception dependentsException = dependents.stream().map(Task::getLastException).filter(Objects::nonNull).findAny().orElse(null);
if (!doDependentsSucceeded && task.isRelyingOnDependents() || canceled) { if (!doDependentsSucceeded && task.isRelyingOnDependents() || canceled) {
task.setLastException(dependentsException); task.setLastException(dependentsException);
throw new SilentException(); throw new CancellationException();
} }
if (doDependentsSucceeded) if (doDependentsSucceeded)
@ -187,11 +187,6 @@ public final class TaskExecutor {
Collection<? extends Task> dependencies = task.getDependencies(); Collection<? extends Task> dependencies = task.getDependencies();
boolean doDependenciesSucceeded = executeTasks(dependencies); boolean doDependenciesSucceeded = executeTasks(dependencies);
Exception dependenciesException = dependencies.stream().map(Task::getLastException).filter(Objects::nonNull).findAny().orElse(null); Exception dependenciesException = dependencies.stream().map(Task::getLastException).filter(Objects::nonNull).findAny().orElse(null);
if (!doDependenciesSucceeded && task.isRelyingOnDependencies()) {
Logging.LOG.severe("Subtasks failed for " + task.getName());
task.setLastException(dependenciesException);
throw new SilentException();
}
if (doDependenciesSucceeded) if (doDependenciesSucceeded)
task.setDependenciesSucceeded(); task.setDependenciesSucceeded();
@ -207,6 +202,12 @@ public final class TaskExecutor {
} }
} }
if (!doDependenciesSucceeded && task.isRelyingOnDependencies()) {
Logging.LOG.severe("Subtasks failed for " + task.getName());
task.setLastException(dependenciesException);
throw new CancellationException();
}
flag = true; flag = true;
if (task.getSignificance().shouldLog()) { if (task.getSignificance().shouldLog()) {
Logging.LOG.log(Level.FINER, "Task finished: " + task.getName()); Logging.LOG.log(Level.FINER, "Task finished: " + task.getName());
@ -221,8 +222,9 @@ public final class TaskExecutor {
} }
task.onDone().fireEvent(new TaskEvent(this, task, true)); task.onDone().fireEvent(new TaskEvent(this, task, true));
taskListeners.forEach(it -> it.onFailed(task, e)); taskListeners.forEach(it -> it.onFailed(task, e));
} catch (SilentException | RejectedExecutionException e) { } catch (CancellationException | RejectedExecutionException e) {
task.setLastException(e); if (task.getLastException() == null)
task.setLastException(e);
} catch (Exception e) { } catch (Exception e) {
task.setLastException(e); task.setLastException(e);
lastException = e; lastException = e;