Skip to content

Commit

Permalink
Improve feedback in case of parsing error in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
maxidorius committed Feb 11, 2019
1 parent bd4ccbc commit 8afdb3e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
42 changes: 23 additions & 19 deletions src/main/java/io/kamax/mxisd/MxisdStandaloneExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,40 @@

import io.kamax.mxisd.config.MxisdConfig;
import io.kamax.mxisd.config.YamlConfigLoader;
import io.kamax.mxisd.exception.ConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;

public class MxisdStandaloneExec {

private static final Logger log = LoggerFactory.getLogger("");
private static final Logger log = LoggerFactory.getLogger("App");

public static void main(String[] args) throws IOException {
log.info("------------- mxisd starting -------------");
MxisdConfig cfg = null;
public static void main(String[] args) {
try {
log.info("------------- mxisd starting -------------");
MxisdConfig cfg = null;

Iterator<String> argsIt = Arrays.asList(args).iterator();
while (argsIt.hasNext()) {
String arg = argsIt.next();
if (StringUtils.equals("-c", arg)) {
String cfgFile = argsIt.next();
cfg = YamlConfigLoader.loadFromFile(cfgFile);
} else {
log.info("Invalid argument: {}", arg);
System.exit(1);
Iterator<String> argsIt = Arrays.asList(args).iterator();
while (argsIt.hasNext()) {
String arg = argsIt.next();
if (StringUtils.equals("-c", arg)) {
String cfgFile = argsIt.next();
cfg = YamlConfigLoader.loadFromFile(cfgFile);
} else {
log.info("Invalid argument: {}", arg);
System.exit(1);
}
}
}

if (Objects.isNull(cfg)) {
cfg = YamlConfigLoader.tryLoadFromFile("mxisd.yaml").orElseGet(MxisdConfig::new);
}
if (Objects.isNull(cfg)) {
cfg = YamlConfigLoader.tryLoadFromFile("mxisd.yaml").orElseGet(MxisdConfig::new);
}

try {
HttpMxisd mxisd = new HttpMxisd(cfg);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
mxisd.stop();
Expand All @@ -64,6 +64,10 @@ public static void main(String[] args) throws IOException {
mxisd.start();

log.info("------------- mxisd started -------------");
} catch (ConfigurationException e) {
log.error(e.getDetailedMessage());
log.error(e.getMessage());
System.exit(2);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/kamax/mxisd/config/YamlConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
package io.kamax.mxisd.config;

import io.kamax.matrix.json.GsonUtil;
import io.kamax.mxisd.exception.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.parser.ParserException;
import org.yaml.snakeyaml.representer.Representer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -37,12 +40,13 @@ public class YamlConfigLoader {
private static final Logger log = LoggerFactory.getLogger(YamlConfigLoader.class);

public static MxisdConfig loadFromFile(String path) throws IOException {
log.debug("Reading config from {}", path);
File f = new File(path).getAbsoluteFile();
log.info("Reading config from {}", f.toString());
Representer rep = new Representer();
rep.getPropertyUtils().setAllowReadOnlyProperties(true);
rep.getPropertyUtils().setSkipMissingProperties(true);
Yaml yaml = new Yaml(new Constructor(MxisdConfig.class), rep);
try (FileInputStream is = new FileInputStream(path)) {
try (FileInputStream is = new FileInputStream(f)) {
MxisdConfig raw = yaml.load(is);
log.debug("Read config in memory from {}", path);

Expand All @@ -53,6 +57,8 @@ public static MxisdConfig loadFromFile(String path) throws IOException {

log.info("Loaded config from {}", path);
return cfg;
} catch (ParserException t) {
throw new ConfigurationException(t.getMessage(), "Could not parse YAML config file - Please check indentation and that the configuration options exist");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@

package io.kamax.mxisd.exception;

import java.util.Optional;

public class ConfigurationException extends RuntimeException {

private String key;
private String detailedMsg;

public ConfigurationException(String key) {
Expand All @@ -40,8 +37,8 @@ public ConfigurationException(String key, String detailedMsg) {
this.detailedMsg = detailedMsg;
}

public Optional<String> getDetailedMessage() {
return Optional.ofNullable(detailedMsg);
public String getDetailedMessage() {
return detailedMsg;
}

}

0 comments on commit 8afdb3e

Please sign in to comment.