QosErrorDecoder implements ErrorDecoder { @Override public Exception decode(String methodKey, Response response) { Optional<Exception> exception = QosExceptionResponseMapper.mapResponseCodeHeaderStream( response.status(), header -> Optional.ofNullable(response.headers().get(header)) .map(Collection::stream) .orElseGet(Stream::empty)) .map(Function.identity()); return exception.orElseGet(() -> delegate.decode(methodKey, response)); } QosErrorDecoder(ErrorDecoder delegate); @Override Exception decode(String methodKey, Response response);  }
@Test public void http_429_throw_qos_throttle() { Map<String, Collection<String>> headers = ImmutableMap.of(); Response response = Response.create(429, "too many requests", headers, new byte[0]); assertThat(decoder.decode(methodKey, response)) .isInstanceOfSatisfying(QosException.Throttle.class, e -> assertThat(e.getRetryAfter()) .isEmpty()); }
@Test public void http_429_throw_qos_throttle_with_retry_after() { Map<String, Collection<String>> headers = ImmutableMap.of(HttpHeaders.RETRY_AFTER, ImmutableList.of("5")); Response response = Response.create(429, "too many requests", headers, new byte[0]); assertThat(decoder.decode(methodKey, response)) .isInstanceOfSatisfying(QosException.Throttle.class, e -> assertThat(e.getRetryAfter()) .contains(Duration.ofSeconds(5))); }
@Test public void http_503_throw_qos_unavailable() { Map<String, Collection<String>> headers = ImmutableMap.of(); Response response = Response.create(503, "too many requests", headers, new byte[0]); assertThat(decoder.decode(methodKey, response)).isInstanceOf(QosException.Unavailable.class); }