Skip to content

Commit

Permalink
Nicer error reporting on invalid configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Mar 13, 2017
1 parent 94a05b0 commit d45f78c
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions rapidoid-commons/src/main/java/org/rapidoid/config/ConfigUtil.java
Expand Up @@ -42,17 +42,36 @@ public class ConfigUtil extends RapidoidThing {
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> parse(byte[] bytes) {
if (new String(bytes).trim().isEmpty()) return U.map();
return Parse.data(bytes, Map.class);
}
};

static synchronized void load(String filename, Config config, List<String> loaded) {
byte[] bytes = tryToLoad(filename, loaded);
Res res = findConfigResource(filename);

byte[] bytes = null;
String realFilename = filename;

if (res.exists()) {
realFilename = res.getCachedFileName();
bytes = res.getBytes();
loaded.add(realFilename);
}

if (bytes != null) {
if (bytes.length > 0) {
Map<String, Object> configData = U.safe(YAML_OR_JSON_PARSER.parse(bytes));
Log.debug("Loading configuration file", "filename", filename);
Map<String, Object> configData;

try {
Log.debug("Loading configuration file", "filename", filename);
configData = U.safe(YAML_OR_JSON_PARSER.parse(bytes));

} catch (Exception e) {
configData = U.map();
Log.error("Couldn't parse configuration file!", "filename", realFilename);
}

config.update(configData);
}

Expand All @@ -62,16 +81,6 @@ static synchronized void load(String filename, Config config, List<String> loade

}

private static byte[] tryToLoad(String filename, List<String> loaded) {
Res res = findConfigResource(filename);

if (res.exists()) {
loaded.add(res.getCachedFileName());
}

return res.getBytesOrNull();
}

private static Res findConfigResource(String filename) {
if (filename.endsWith(YML_OR_YAML_OR_JSON)) {

Expand Down

0 comments on commit d45f78c

Please sign in to comment.