Skip to content

Commit 00039d8

Browse files
committed
Rationalize exception handling
1 parent 76c2ab2 commit 00039d8

File tree

4 files changed

+11
-41
lines changed

4 files changed

+11
-41
lines changed

src/main/java/com/rabbitmq/client/amqp/impl/AmqpConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private org.apache.qpid.protonj2.client.Connection connect(
205205
checkBrokerVersion(connection);
206206
return connection;
207207
} catch (ClientException e) {
208-
throw ExceptionUtils.convertOnConnection(e);
208+
throw ExceptionUtils.convert(e);
209209
} finally {
210210
LOGGER.debug("Connection attempt took {}", stopWatch.stop());
211211
}
@@ -256,6 +256,7 @@ TopologyListener createTopologyListener(AmqpConnectionBuilder builder) {
256256
return;
257257
}
258258
AmqpException exception = ExceptionUtils.convert(event.failureCause());
259+
LOGGER.debug("Converted native exception to {}", exception.getClass().getSimpleName());
259260

260261
if (RECOVERY_PREDICATE.test(exception) && this.state() != OPENING) {
261262
LOGGER.debug("Queueing recovery task, error is {}", exception.getMessage());

src/main/java/com/rabbitmq/client/amqp/impl/ExceptionUtils.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,6 @@ static AmqpException convert(ClientException e) {
5151
return convert(e, null);
5252
}
5353

54-
static AmqpException convertOnConnection(ClientException e) {
55-
if (e instanceof ClientConnectionRemotelyClosedException) {
56-
if (isNetworkError(e)) {
57-
throw new AmqpException.AmqpConnectionException(e.getMessage(), e);
58-
} else {
59-
// likely a TLS error
60-
return new AmqpException.AmqpSecurityException(
61-
e.getCause() instanceof SSLException ? e.getCause() : e);
62-
}
63-
} else {
64-
return convert(e);
65-
}
66-
}
67-
6854
static AmqpException convert(ExecutionException e) {
6955
if (e.getCause() instanceof ClientException) {
7056
return convert((ClientException) e.getCause());
@@ -78,7 +64,7 @@ static AmqpException convert(ClientException e, String format, Object... args) {
7864
if (e.getCause() instanceof SSLException) {
7965
return new AmqpException.AmqpSecurityException(message, e.getCause());
8066
} else if (e instanceof ClientConnectionSecurityException) {
81-
throw new AmqpException.AmqpSecurityException(message, e);
67+
return new AmqpException.AmqpSecurityException(message, e);
8268
} else if (isNetworkError(e)) {
8369
return new AmqpException.AmqpConnectionException(e.getMessage(), e);
8470
} else if (e instanceof ClientSessionRemotelyClosedException) {
@@ -101,14 +87,17 @@ static AmqpException convert(ClientException e, String format, Object... args) {
10187
return new AmqpException.AmqpResourceClosedException(e.getMessage(), e);
10288
}
10389
} else if (e instanceof ClientConnectionRemotelyClosedException) {
104-
if (!isUnauthorizedAccess(
105-
((ClientConnectionRemotelyClosedException) e).getErrorCondition())) {
90+
ErrorCondition errorCondition =
91+
((ClientConnectionRemotelyClosedException) e).getErrorCondition();
92+
if (isNetworkError(e) || !isUnauthorizedAccess(errorCondition)) {
10693
return new AmqpException.AmqpConnectionException(e.getMessage(), e);
94+
} else if (e.getCause() instanceof SSLException) {
95+
return new AmqpException.AmqpSecurityException(e.getCause());
10796
} else {
10897
return new AmqpException(e.getMessage(), e);
10998
}
11099
} else {
111-
return new AmqpException(e);
100+
return new AmqpException(message, e);
112101
}
113102
}
114103

src/test/java/com/rabbitmq/client/amqp/impl/ExceptionUtilsTest.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,13 @@
2121
import static org.assertj.core.api.Assertions.assertThat;
2222

2323
import com.rabbitmq.client.amqp.AmqpException;
24-
import javax.net.ssl.SSLException;
2524
import org.apache.qpid.protonj2.client.ErrorCondition;
26-
import org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException;
27-
import org.apache.qpid.protonj2.client.exceptions.ClientException;
2825
import org.apache.qpid.protonj2.client.exceptions.ClientLinkRemotelyClosedException;
2926
import org.apache.qpid.protonj2.client.exceptions.ClientSessionRemotelyClosedException;
3027
import org.junit.jupiter.api.Test;
3128

3229
public class ExceptionUtilsTest {
3330

34-
@Test
35-
void convertOnConnectionTest() {
36-
assertThat(
37-
convertOnConnection(
38-
new ClientConnectionRemotelyClosedException("", new RuntimeException())))
39-
.isInstanceOf(AmqpException.AmqpSecurityException.class)
40-
.hasCauseInstanceOf(ClientConnectionRemotelyClosedException.class);
41-
assertThat(
42-
convertOnConnection(
43-
new ClientConnectionRemotelyClosedException("", new SSLException(""))))
44-
.isInstanceOf(AmqpException.AmqpSecurityException.class)
45-
.hasCauseInstanceOf(SSLException.class);
46-
assertThat(convertOnConnection(new ClientException("")))
47-
.isInstanceOf(AmqpException.class)
48-
.hasCauseInstanceOf(ClientException.class);
49-
}
50-
5131
@Test
5232
void convertTest() {
5333
assertThat(

src/test/resources/logback-test.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
</appender>
77

88
<logger name="com.rabbitmq.client.amqp" level="warn" />
9-
<logger name="com.rabbitmq.client.amqp.EntityRecovery" level="warn" />
10-
<logger name="com.rabbitmq.client.amqp.AmqpConnection" level="warn" />
9+
<logger name="com.rabbitmq.client.amqp.impl.EntityRecovery" level="warn" />
10+
<logger name="com.rabbitmq.client.amqp.impl.AmqpConnection" level="warn" />
1111
<logger name="org.apache.qpid" level="warn" />
1212

1313
<root level="warn">

0 commit comments

Comments
 (0)