Friendly prompt if response code is not OK

This commit is contained in:
huanghongxun 2019-04-03 14:05:14 +08:00
parent 8c7580488e
commit b198eea9e4
7 changed files with 63 additions and 5 deletions

View File

@ -34,6 +34,8 @@ import org.jackhuang.hmcl.ui.construct.MessageDialogPane.MessageType;
import org.jackhuang.hmcl.ui.wizard.WizardController; import org.jackhuang.hmcl.ui.wizard.WizardController;
import org.jackhuang.hmcl.ui.wizard.WizardProvider; import org.jackhuang.hmcl.ui.wizard.WizardProvider;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.i18n.I18n;
import org.jackhuang.hmcl.util.io.ResponseCodeException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.util.Map; import java.util.Map;
@ -128,6 +130,13 @@ public final class InstallerWizardProvider implements WizardProvider {
} else if (exception instanceof DownloadException) { } else if (exception instanceof DownloadException) {
if (exception.getCause() instanceof SocketTimeoutException) { if (exception.getCause() instanceof SocketTimeoutException) {
Controllers.dialog(i18n("install.failed.downloading.timeout", ((DownloadException) exception).getUrl()), i18n("install.failed.downloading"), MessageType.ERROR, next); Controllers.dialog(i18n("install.failed.downloading.timeout", ((DownloadException) exception).getUrl()), i18n("install.failed.downloading"), MessageType.ERROR, next);
} else if (exception.getCause() instanceof ResponseCodeException) {
ResponseCodeException responseCodeException = (ResponseCodeException) exception.getCause();
if (I18n.hasKey("download.code." + responseCodeException.getResponseCode())) {
Controllers.dialog(i18n("download.code." + responseCodeException.getResponseCode()) + ", " + ((DownloadException) exception).getUrl() + "\n" + StringUtils.getStackTrace(exception.getCause()), i18n("install.failed.downloading"), MessageType.ERROR, next);
} else {
Controllers.dialog(i18n("install.failed.downloading.detail", ((DownloadException) exception).getUrl()) + "\n" + StringUtils.getStackTrace(exception.getCause()), i18n("install.failed.downloading"), MessageType.ERROR, next);
}
} else { } else {
Controllers.dialog(i18n("install.failed.downloading.detail", ((DownloadException) exception).getUrl()) + "\n" + StringUtils.getStackTrace(exception.getCause()), i18n("install.failed.downloading"), MessageType.ERROR, next); Controllers.dialog(i18n("install.failed.downloading.detail", ((DownloadException) exception).getUrl()) + "\n" + StringUtils.getStackTrace(exception.getCause()), i18n("install.failed.downloading"), MessageType.ERROR, next);
} }

View File

@ -57,4 +57,12 @@ public final class I18n {
} }
} }
public static boolean hasKey(String key) {
try {
getResourceBundle().getString(key);
return true;
} catch (MissingResourceException e) {
return false;
}
}
} }

View File

@ -91,6 +91,7 @@ crash.NoClassDefFound=Please check "HMCL" software is complete.
crash.user_fault=Your OS or Java environment may not be properly installed resulting in crashing of this software, please check your Java Environment or your computer! crash.user_fault=Your OS or Java environment may not be properly installed resulting in crashing of this software, please check your Java Environment or your computer!
download=Download download=Download
download.code.404=File does not found in remote server
download.failed=Failed to download download.failed=Failed to download
download.failed.empty=No candidates. Click here to return. download.failed.empty=No candidates. Click here to return.
download.failed.refresh=Unable to load version list. Click here to retry. download.failed.refresh=Unable to load version list. Click here to retry.

View File

@ -90,6 +90,7 @@ crash.NoClassDefFound=請確認 HMCL 本體是否完整,或更新您的 Java
crash.user_fault=您的系統或 Java 環境可能安裝不當導致本軟體崩潰,請檢查您的 Java 環境或您的電腦!可以嘗試重新安裝 Java。 crash.user_fault=您的系統或 Java 環境可能安裝不當導致本軟體崩潰,請檢查您的 Java 環境或您的電腦!可以嘗試重新安裝 Java。
download=下載 download=下載
download.code.404=遠程伺服器不包含需要下載的文件
download.failed=下載失敗 download.failed=下載失敗
download.failed.empty=沒有可供安裝的版本,點擊此處返回。 download.failed.empty=沒有可供安裝的版本,點擊此處返回。
download.failed.refresh=載入版本列表失敗,點擊此處重試。 download.failed.refresh=載入版本列表失敗,點擊此處重試。

View File

@ -90,6 +90,7 @@ crash.NoClassDefFound=请确认 HMCL 本体是否完整,或更新您的 Java
crash.user_fault=您的系统或 Java 环境可能安装不当导致本软件崩溃,请检查您的 Java 环境或您的电脑!可以尝试重新安装 Java。 crash.user_fault=您的系统或 Java 环境可能安装不当导致本软件崩溃,请检查您的 Java 环境或您的电脑!可以尝试重新安装 Java。
download=下载 download=下载
download.code.404=远程服务器不包含需要下载的文件
download.failed=下载失败 download.failed=下载失败
download.failed.empty=没有可供安装的版本,点击此处返回。 download.failed.empty=没有可供安装的版本,点击此处返回。
download.failed.refresh=加载版本列表失败,点击此处重试。 download.failed.refresh=加载版本列表失败,点击此处重试。

View File

@ -21,10 +21,7 @@ import org.jackhuang.hmcl.event.EventManager;
import org.jackhuang.hmcl.event.FailedEvent; import org.jackhuang.hmcl.event.FailedEvent;
import org.jackhuang.hmcl.util.CacheRepository; import org.jackhuang.hmcl.util.CacheRepository;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.io.ChecksumMismatchException; import org.jackhuang.hmcl.util.io.*;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.IOUtils;
import org.jackhuang.hmcl.util.io.NetworkUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -233,7 +230,7 @@ public class FileDownloadTask extends Task {
repository.removeRemoteEntry(con); repository.removeRemoteEntry(con);
} }
} else if (con.getResponseCode() / 100 != 2) { } else if (con.getResponseCode() / 100 != 2) {
throw new IOException("Server error, response code: " + con.getResponseCode()); throw new ResponseCodeException(currentURL, con.getResponseCode());
} }
int contentLength = con.getContentLength(); int contentLength = con.getContentLength();

View File

@ -0,0 +1,41 @@
/*
* 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.util.io;
import java.io.IOException;
import java.net.URL;
public class ResponseCodeException extends IOException {
private final URL url;
private final int responseCode;
public ResponseCodeException(URL url, int responseCode) {
super("Unable to request url " + url + ", response code: " + responseCode);
this.url = url;
this.responseCode = responseCode;
}
public URL getUrl() {
return url;
}
public int getResponseCode() {
return responseCode;
}
}