Should fix NPE when setting up connection to GitHub

This commit is contained in:
Florian CUNY 2019-09-28 14:21:16 +02:00
parent 4eae5fada0
commit d9dc337de9

View File

@ -59,36 +59,27 @@ public class WebManager {
if (plugin.getSettings().isLogGithubDownloadData()) {
plugin.log("Downloading data from GitHub...");
}
GitHubRepository repo = new GitHubRepository(gh, "BentoBoxWorld/weblink");
GitHubRepository repo;
try {
repo = new GitHubRepository(gh, "BentoBoxWorld/weblink");
} catch (Exception e) {
plugin.logError("An unhandled exception occurred when trying to connect to GitHub...");
plugin.logStacktrace(e);
String tagsContent = "";
String topicsContent = "";
String catalogContent = "";
// Stop the execution of the method right away.
return;
}
// Downloading the data
try {
tagsContent = repo.getContent("catalog/tags.json").getContent().replaceAll("\\n", "");
topicsContent = repo.getContent("catalog/topics.json").getContent().replaceAll("\\n", "");
catalogContent = repo.getContent("catalog/catalog.json").getContent().replaceAll("\\n", "");
} catch (IllegalAccessException e) {
if (plugin.getSettings().isLogGithubDownloadData()) {
plugin.log("Could not connect to GitHub.");
}
} catch (Exception e) {
plugin.logError("An unhandled exception occurred when downloading data from GitHub...");
plugin.logStacktrace(e);
}
String tagsContent = getContent(repo, "catalog/tags.json");
String topicsContent = getContent(repo, "catalog/topics.json");
String catalogContent = getContent(repo, "catalog/catalog.json");
// People were concerned that the download took ages, so we need to tell them it's over now.
if (plugin.getSettings().isLogGithubDownloadData()) {
plugin.log("Successfully downloaded data from GitHub.");
}
// Decoding the Base64 encoded contents
tagsContent = new String(Base64.getDecoder().decode(tagsContent), StandardCharsets.UTF_8);
topicsContent = new String(Base64.getDecoder().decode(topicsContent), StandardCharsets.UTF_8);
catalogContent = new String(Base64.getDecoder().decode(catalogContent), StandardCharsets.UTF_8);
/* Parsing the data */
// Register the tags translations in the locales
@ -140,6 +131,29 @@ public class WebManager {
});
}
/**
*
* @param repo
* @param fileName
* @return
* @since 1.8.0
*/
@NonNull
private String getContent(@NonNull GitHubRepository repo, String fileName) {
try {
String content = repo.getContent(fileName).getContent().replaceAll("\\n", "");
return new String(Base64.getDecoder().decode(content), StandardCharsets.UTF_8);
} catch (IllegalAccessException e) {
if (plugin.getSettings().isLogGithubDownloadData()) {
plugin.log("Could not connect to GitHub.");
}
} catch (Exception e) {
plugin.logError("An unhandled exception occurred when downloading '" + fileName + "' from GitHub...");
plugin.logStacktrace(e);
}
return "";
}
/**
* Returns the contents of the addons catalog (may be an empty list).
* @return the contents of the addons catalog.