Skip to content

Commit

Permalink
Sort after filter, so total order exists
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmv committed Feb 27, 2024
1 parent 3677c04 commit fd8a182
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions container-core/src/main/java/com/yahoo/restapi/RestApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ private HttpResponse mapException(RequestContextImpl context, RuntimeException e
log.log(Level.FINE, e, e::getMessage);
ExceptionMapperHolder<?> mapper = exceptionMappers.stream()
.filter(holder -> holder.type.isAssignableFrom(e.getClass()))
.findFirst().orElseThrow(() -> e);
// Topologically sort children before superclasses, so most the specific match is found by iterating through mappers in order.
.min((a, b) -> (a.type.isAssignableFrom(b.type) ? 1 : 0) + (b.type.isAssignableFrom(a.type) ? -1 : 0))
.orElseThrow(() -> e);
return mapper.toResponse(context, e);
}

Expand Down Expand Up @@ -210,14 +212,6 @@ private static List<ExceptionMapperHolder<?>> combineWithDefaultExceptionMappers
if (!disableDefaultMappers){
exceptionMappers.addAll(RestApiMappers.DEFAULT_EXCEPTION_MAPPERS);
}
// Topologically sort children before superclasses, so most the specific match is found by iterating through mappers in order.
exceptionMappers.sort((l, r) -> {
if (l.type.equals(r.type)) return 0;
if (l.type.isAssignableFrom(r.type)) return 1;
if (r.type.isAssignableFrom(l.type)) return -1;
// Ensure global ordering when no inheritance relationship exists
return l.type.getName().compareTo(r.type.getName());
});
return exceptionMappers;
}

Expand Down

0 comments on commit fd8a182

Please sign in to comment.