Skip to content

Commit

Permalink
Add reason to propagated QoS errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoenig10 committed Jan 3, 2024
1 parent e887725 commit e6c8d02
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package com.palantir.conjure.java.dialogue.serde;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.io.CharStreams;
import com.google.common.net.HttpHeaders;
import com.google.common.primitives.Longs;
import com.palantir.conjure.java.api.errors.QosException;
import com.palantir.conjure.java.api.errors.QosReason;
import com.palantir.conjure.java.api.errors.RemoteException;
import com.palantir.conjure.java.api.errors.SerializableError;
import com.palantir.conjure.java.api.errors.UnknownRemoteException;
Expand Down Expand Up @@ -57,6 +59,9 @@ public enum ErrorDecoder {
private static final SafeLogger log = SafeLoggerFactory.get(ErrorDecoder.class);
private static final ObjectMapper MAPPER = ObjectMappers.newClientObjectMapper();

@VisibleForTesting
static final QosReason QOS_REASON = QosReason.of("client-qos-response");

public boolean isError(Response response) {
return 300 <= response.code() && response.code() <= 599;
}
Expand Down Expand Up @@ -99,10 +104,10 @@ private RuntimeException decodeInternal(Response response) {
return response.getFirstHeader(HttpHeaders.RETRY_AFTER)
.map(Longs::tryParse)
.map(Duration::ofSeconds)
.map(QosException::throttle)
.orElseGet(QosException::throttle);
.map(duration -> QosException.throttle(QOS_REASON, duration))
.orElseGet(() -> QosException.throttle(QOS_REASON));
case 503:
return QosException.unavailable();
return QosException.unavailable(QOS_REASON);
}

String body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ public void testQos503() {
assertThat(decoder.isError(response)).isTrue();

RuntimeException result = decoder.decode(response);
assertThat(result).isInstanceOf(QosException.Unavailable.class);
assertThat(result).isInstanceOfSatisfying(QosException.Unavailable.class, exception -> {
assertThat(exception.getReason()).isEqualTo(ErrorDecoder.QOS_REASON);
});
}

@Test
Expand All @@ -105,9 +107,10 @@ public void testQos429() {
assertThat(decoder.isError(response)).isTrue();

RuntimeException result = decoder.decode(response);
assertThat(result)
.isInstanceOfSatisfying(QosException.Throttle.class, exception -> assertThat(exception.getRetryAfter())
.isEmpty());
assertThat(result).isInstanceOfSatisfying(QosException.Throttle.class, exception -> {
assertThat(exception.getReason()).isEqualTo(ErrorDecoder.QOS_REASON);
assertThat(exception.getRetryAfter()).isEmpty();
});
}

@Test
Expand All @@ -117,9 +120,10 @@ public void testQos429_retryAfter() {
assertThat(decoder.isError(response)).isTrue();

RuntimeException result = decoder.decode(response);
assertThat(result)
.isInstanceOfSatisfying(QosException.Throttle.class, exception -> assertThat(exception.getRetryAfter())
.hasValue(Duration.ofSeconds(3)));
assertThat(result).isInstanceOfSatisfying(QosException.Throttle.class, exception -> {
assertThat(exception.getReason()).isEqualTo(ErrorDecoder.QOS_REASON);
assertThat(exception.getRetryAfter()).hasValue(Duration.ofSeconds(3));
});
}

@Test
Expand All @@ -129,9 +133,10 @@ public void testQos429_retryAfter_invalid() {
assertThat(decoder.isError(response)).isTrue();

RuntimeException result = decoder.decode(response);
assertThat(result)
.isInstanceOfSatisfying(QosException.Throttle.class, exception -> assertThat(exception.getRetryAfter())
.isEmpty());
assertThat(result).isInstanceOfSatisfying(QosException.Throttle.class, exception -> {
assertThat(exception.getReason()).isEqualTo(ErrorDecoder.QOS_REASON);
assertThat(exception.getRetryAfter()).isEmpty();
});
}

@Test
Expand Down

0 comments on commit e6c8d02

Please sign in to comment.