Skip to content

Commit

Permalink
gives preferred status to first mapper errors - refs swagger-api#3911
Browse files Browse the repository at this point in the history
  • Loading branch information
adagios committed Apr 20, 2021
1 parent 123577e commit 592cfa4
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,35 @@ private <T> Optional<T> readStructuredDataFromFile(String filePath, Class<T> out
List<BiFunction<String, Class<T>, T>> mappers = getSortedMappers(pathObj);

T instance = null;
Throwable caughtEx = null;
List<Throwable> caughtExs = new ArrayList<>();

// iterate through mappers and see if one is able to parse
for (BiFunction<String, Class<T>, T> mapper : mappers) {
try {
instance = mapper.apply(fileContent, outputClass);
break;
} catch (Exception e) {
caughtEx = e;
caughtExs.add(e);
}
}

// if no mapper could read the content correctly, finish with error
if (instance == null) {
if (caughtEx == null) {
caughtEx = new IllegalStateException("undefined state");
if (caughtExs.isEmpty()) {
caughtExs.add(new IllegalStateException("undefined state"));
}
getLog().error(format("Could not read file '%s' for config %s", pathObj.toString(), configName), caughtEx);


// we give more importance to the first exception, it was produced by the preferred mapper
Throwable caughtEx = caughtExs.get(0);
getLog().error(format("Could not read file '%s' for config %s", pathObj, configName), caughtEx);

if(caughtExs.size() > 1){
for (Throwable ex : caughtExs.subList(1, caughtExs.size())) {
getLog().warn(format("Also could not read file '%s' for config %s with alternate mapper", pathObj, configName), ex);
}
}

throw new IllegalStateException(caughtEx.getMessage(), caughtEx);
}

Expand Down

0 comments on commit 592cfa4

Please sign in to comment.