Skip to content

Commit 052d14a

Browse files
Return 0 status code on exception or invalid code
fixes gh-1382
1 parent fd35911 commit 052d14a

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/async/ExecutorBeanPostProcessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,8 @@ private static <T> boolean anyFinalMethods(T object, Class<T> iface) {
272272
method -> {
273273
Method m = ReflectionUtils.findMethod(object.getClass(),
274274
method.getName(), method.getParameterTypes());
275-
return m != null &&
276-
!ReflectionUtils.isObjectMethod(m) &&
277-
Modifier.isFinal(m.getModifiers());
275+
return m != null && !ReflectionUtils.isObjectMethod(m)
276+
&& Modifier.isFinal(m.getModifiers());
278277
});
279278
return finalMethodPresent.get();
280279
}

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/TraceWebClientBeanPostProcessor.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,17 @@ public String requestHeader(ClientRequest request, String name) {
395395

396396
@Override
397397
public Integer statusCode(ClientResponse response) {
398-
return response.statusCode().value();
398+
return response.rawStatusCode();
399+
}
400+
401+
@Override
402+
public int statusCodeAsInt(ClientResponse response) {
403+
try {
404+
return super.statusCodeAsInt(response);
405+
}
406+
catch (Exception dontCare) {
407+
return 0;
408+
}
399409
}
400410

401411
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2013-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.sleuth.instrument.web.client;
18+
19+
import org.assertj.core.api.BDDAssertions;
20+
import org.junit.Test;
21+
import org.mockito.BDDMockito;
22+
23+
import org.springframework.web.reactive.function.client.ClientResponse;
24+
25+
public class TraceExchangeFilterFunctionHttpAdapterTests {
26+
27+
@Test
28+
public void should_return_0_when_invalid_status_code_is_returned() {
29+
ClientResponse clientResponse = BDDMockito.mock(ClientResponse.class);
30+
BDDMockito.given(clientResponse.rawStatusCode())
31+
.willThrow(new IllegalStateException("Boom"));
32+
TraceExchangeFilterFunction.HttpAdapter adapter = new TraceExchangeFilterFunction.HttpAdapter();
33+
34+
Integer statusCode = adapter.statusCodeAsInt(clientResponse);
35+
36+
BDDAssertions.then(statusCode).isZero();
37+
}
38+
39+
@Test
40+
public void should_return_status_code_when_valid_status_code_is_returned() {
41+
ClientResponse clientResponse = BDDMockito.mock(ClientResponse.class);
42+
BDDMockito.given(clientResponse.rawStatusCode()).willReturn(200);
43+
TraceExchangeFilterFunction.HttpAdapter adapter = new TraceExchangeFilterFunction.HttpAdapter();
44+
45+
Integer statusCode = adapter.statusCodeAsInt(clientResponse);
46+
47+
BDDAssertions.then(statusCode).isEqualTo(200);
48+
}
49+
50+
}

0 commit comments

Comments
 (0)