fix: Fix startup exception and theme imports in dev environment (#916)

* Log error instead of throwing while starting halo

* Provide unknown version support

* Fix code format
pull/917/head
John Niang 2020-06-15 21:45:01 +08:00 committed by GitHub
parent 73362869b7
commit 658f0a2b3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 24 deletions

View File

@ -74,7 +74,6 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
private void printStartInfo() {
String blogUrl = optionService.getBlogBaseUrl();
log.info(AnsiOutput.toString(AnsiColor.BRIGHT_BLUE, "Halo started at ", blogUrl));
log.info(AnsiOutput.toString(AnsiColor.BRIGHT_BLUE, "Halo admin started at ", blogUrl, "/", haloProperties.getAdminPath()));
if (!haloProperties.isDocDisabled()) {
@ -149,7 +148,7 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
log.debug("Skipped copying theme folder due to existence of theme folder");
}
} catch (Exception e) {
throw new RuntimeException("Initialize internal theme to user path error", e);
log.error("Initialize internal theme to user path error!", e);
}
}
@ -188,7 +187,6 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
Files.createDirectories(dataExportPath);
log.info("Created data export directory: [{}]", dataExportPath);
}
} catch (IOException ie) {
throw new RuntimeException("Failed to initialize directories", ie);
}

View File

@ -3,6 +3,7 @@ package run.halo.app.model.support;
import org.springframework.http.HttpHeaders;
import java.io.File;
import java.util.Optional;
/**
* <pre>
@ -139,6 +140,12 @@ public class HaloConst {
* Version constant. (Available in production environment)
*/
public static final String HALO_VERSION;
/**
* Unknown version: unknown
*/
public static final String UNKNOWN_VERSION = "unknown";
/**
* Database product name.
*/
@ -150,6 +157,6 @@ public class HaloConst {
static {
// Set version
HALO_VERSION = HaloConst.class.getPackage().getImplementationVersion();
HALO_VERSION = Optional.ofNullable(HaloConst.class.getPackage().getImplementationVersion()).orElse(UNKNOWN_VERSION);
}
}

View File

@ -1,43 +1,63 @@
package run.halo.app.utils;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import run.halo.app.model.support.HaloConst;
import java.util.Objects;
import java.util.StringTokenizer;
/**
* @author ryanwang
* @date 2020-02-03
* @see com.sun.xml.internal.ws.util.VersionUtil
* @see "com.sun.xml.internal.ws.util.VersionUtil"
*/
@Slf4j
public class VersionUtil {
public VersionUtil() {
private static final String UNDERLINE = "_";
private VersionUtil() {
}
public static int[] getCanonicalVersion(String version) {
Assert.hasText(version, "Version must not be blank");
if (Objects.equals(version, HaloConst.UNKNOWN_VERSION)) {
log.warn("Unknown version will be converted to {}.{}.{}.{}",
Integer.MAX_VALUE,
Integer.MAX_VALUE,
Integer.MAX_VALUE,
Integer.MAX_VALUE);
return new int[] {Integer.MAX_VALUE,
Integer.MAX_VALUE,
Integer.MAX_VALUE,
Integer.MAX_VALUE};
}
int[] canonicalVersion = new int[] {1, 1, 0, 0};
StringTokenizer tokenizer = new StringTokenizer(version, ".");
String token = tokenizer.nextToken();
canonicalVersion[0] = Integer.parseInt(token);
token = tokenizer.nextToken();
StringTokenizer subTokenizer;
if (!token.contains(StrUtil.UNDERLINE)) {
if (!token.contains(UNDERLINE)) {
canonicalVersion[1] = Integer.parseInt(token);
} else {
subTokenizer = new StringTokenizer(token, "_");
subTokenizer = new StringTokenizer(token, UNDERLINE);
canonicalVersion[1] = Integer.parseInt(subTokenizer.nextToken());
canonicalVersion[3] = Integer.parseInt(subTokenizer.nextToken());
}
if (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
if (!token.contains(StrUtil.UNDERLINE)) {
if (!token.contains(UNDERLINE)) {
canonicalVersion[2] = Integer.parseInt(token);
if (tokenizer.hasMoreTokens()) {
canonicalVersion[3] = Integer.parseInt(tokenizer.nextToken());
}
} else {
subTokenizer = new StringTokenizer(token, "_");
subTokenizer = new StringTokenizer(token, UNDERLINE);
canonicalVersion[2] = Integer.parseInt(subTokenizer.nextToken());
canonicalVersion[3] = Integer.parseInt(subTokenizer.nextToken());
}
@ -47,6 +67,8 @@ public class VersionUtil {
}
public static int compare(String version1, String version2) {
log.debug("Comparing version [{}] with [{}]", version1, version2);
int[] canonicalVersion1 = getCanonicalVersion(version1);
int[] canonicalVersion2 = getCanonicalVersion(version2);
if (canonicalVersion1[0] < canonicalVersion2[0]) {

View File

@ -1,26 +1,45 @@
package run.halo.app.utils;
import org.junit.Assert;
import org.junit.Test;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Test;
import run.halo.app.model.support.HaloConst;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author ryanwang
* @date 2020-02-03
*/
public class VersionUtilTest {
class VersionUtilTest {
@Test
public void compareVersion() {
Assert.assertTrue(VersionUtil.compareVersion("1.2.0", "1.1.1"));
void compareVersion() {
assertTrue(VersionUtil.compareVersion("1.2.0", "1.1.1"));
assertTrue(VersionUtil.compareVersion("1.2.1", "1.2.0"));
assertTrue(VersionUtil.compareVersion("1.2.0", "1.1.1.0"));
assertTrue(VersionUtil.compareVersion("1.2.0", "0.4.4"));
assertFalse(VersionUtil.compareVersion("1.1.1", "1.2.0"));
assertFalse(VersionUtil.compareVersion("0.0.1", "1.2.0"));
}
Assert.assertTrue(VersionUtil.compareVersion("1.2.1", "1.2.0"));
@Test
void unknownVersionCompareTest() {
// build a random version
String randomVersion = String.join(".",
RandomStringUtils.randomNumeric(1),
RandomStringUtils.randomNumeric(2),
RandomStringUtils.randomNumeric(3));
VersionUtil.compareVersion(HaloConst.UNKNOWN_VERSION, randomVersion);
}
Assert.assertTrue(VersionUtil.compareVersion("1.2.0", "1.1.1.0"));
Assert.assertTrue(VersionUtil.compareVersion("1.2.0", "0.4.4"));
Assert.assertFalse(VersionUtil.compareVersion("1.1.1", "1.2.0"));
Assert.assertFalse(VersionUtil.compareVersion("0.0.1", "1.2.0"));
@Test
void unknownOrEmptyCanonicalVersionTest() {
assertThrows(IllegalArgumentException.class, () -> VersionUtil.getCanonicalVersion(null));
int[] version = VersionUtil.getCanonicalVersion(HaloConst.UNKNOWN_VERSION);
assertNotNull(version);
assertEquals(4, version.length);
for (int v : version) {
assertEquals(Integer.MAX_VALUE, v);
}
}
}