@Test public void exceptionMayNotBeMappedToMultipleExceptions() { ExceptionStatusMapper mapper = new ExceptionStatusMapper.Builder() .add(BAD_GATEWAY, Exception1.class) .add(GATEWAY_TIMEOUT, DoubleMappedException.class) .build(); LoggingTestSupport support = new LoggingTestSupport(ExceptionStatusMapper.class); Optional<HttpResponseStatus> status; try { status = mapper.statusFor(new DoubleMappedException()); } finally { assertThat(support.lastMessage(), is(loggingEvent(ERROR, "Multiple matching statuses for throwable=" + quote(DoubleMappedException.class.getName()) + " statuses=\\[502 Bad Gateway, 504 Gateway Timeout\\]" ))); } assertThat(status, isAbsent()); }
Optional<HttpResponseStatus> statusFor(Throwable throwable) { List<HttpResponseStatus> matchingStatuses = this.multimap.entries().stream() .filter(entry -> entry.getValue().isInstance(throwable)) .sorted(comparing(entry -> entry.getKey().code())) .map(Map.Entry::getKey) .collect(toList()); if (matchingStatuses.size() > 1) { LOG.error("Multiple matching statuses for throwable={} statuses={}", throwable, matchingStatuses); return Optional.empty(); } return matchingStatuses.stream().findFirst(); }
ExceptionStatusMapper { Optional<HttpResponseStatus> statusFor(Throwable throwable) { List<HttpResponseStatus> matchingStatuses = this.multimap.entries().stream() .filter(entry -> entry.getValue().isInstance(throwable)) .sorted(comparing(entry -> entry.getKey().code())) .map(Map.Entry::getKey) .collect(toList()); if (matchingStatuses.size() > 1) { LOG.error("Multiple matching statuses for throwable={} statuses={}", throwable, matchingStatuses); return Optional.empty(); } return matchingStatuses.stream().findFirst(); } }
ExceptionStatusMapper { Optional<HttpResponseStatus> statusFor(Throwable throwable) { List<HttpResponseStatus> matchingStatuses = this.multimap.entries().stream() .filter(entry -> entry.getValue().isInstance(throwable)) .sorted(comparing(entry -> entry.getKey().code())) .map(Map.Entry::getKey) .collect(toList()); if (matchingStatuses.size() > 1) { LOG.error("Multiple matching statuses for throwable={} statuses={}", throwable, matchingStatuses); return Optional.empty(); } return matchingStatuses.stream().findFirst(); } private  ExceptionStatusMapper(Builder builder); }
ExceptionStatusMapper { Optional<HttpResponseStatus> statusFor(Throwable throwable) { List<HttpResponseStatus> matchingStatuses = this.multimap.entries().stream() .filter(entry -> entry.getValue().isInstance(throwable)) .sorted(comparing(entry -> entry.getKey().code())) .map(Map.Entry::getKey) .collect(toList()); if (matchingStatuses.size() > 1) { LOG.error("Multiple matching statuses for throwable={} statuses={}", throwable, matchingStatuses); return Optional.empty(); } return matchingStatuses.stream().findFirst(); } private  ExceptionStatusMapper(Builder builder);  }
ExceptionStatusMapper { Optional<HttpResponseStatus> statusFor(Throwable throwable) { List<HttpResponseStatus> matchingStatuses = this.multimap.entries().stream() .filter(entry -> entry.getValue().isInstance(throwable)) .sorted(comparing(entry -> entry.getKey().code())) .map(Map.Entry::getKey) .collect(toList()); if (matchingStatuses.size() > 1) { LOG.error("Multiple matching statuses for throwable={} statuses={}", throwable, matchingStatuses); return Optional.empty(); } return matchingStatuses.stream().findFirst(); } private  ExceptionStatusMapper(Builder builder);   }