Skip to content

Commit c761111

Browse files
committed
Include cause when throwing PortInUseException
Update classes that throw `PortInUseException` so that they also include the cause. Prior to this commit the cause was not included which could make diagnosing the real cause difficult. See gh-19807
1 parent 85befdf commit c761111

File tree

6 files changed

+19
-10
lines changed

6 files changed

+19
-10
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void start() throws WebServerException {
148148
}
149149
catch (IOException ex) {
150150
if (connector instanceof NetworkConnector && findBindException(ex) != null) {
151-
throw new PortInUseException(((NetworkConnector) connector).getPort());
151+
throw new PortInUseException(((NetworkConnector) connector).getPort(), ex);
152152
}
153153
throw ex;
154154
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,7 +69,7 @@ public void start() throws WebServerException {
6969
catch (Exception ex) {
7070
ChannelBindException bindException = findBindException(ex);
7171
if (bindException != null) {
72-
throw new PortInUseException(bindException.localPort());
72+
throw new PortInUseException(bindException.localPort(), ex);
7373
}
7474
throw new WebServerException("Unable to start Netty", ex);
7575
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -151,7 +151,7 @@ public void start() throws WebServerException {
151151
List<Port> actualPorts = getActualPorts();
152152
failedPorts.removeAll(actualPorts);
153153
if (failedPorts.size() == 1) {
154-
throw new PortInUseException(failedPorts.iterator().next().getNumber());
154+
throw new PortInUseException(failedPorts.iterator().next().getNumber(), ex);
155155
}
156156
}
157157
throw new WebServerException("Unable to start embedded Undertow", ex);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -109,7 +109,7 @@ public void start() throws WebServerException {
109109
List<UndertowWebServer.Port> actualPorts = getActualPorts();
110110
failedPorts.removeAll(actualPorts);
111111
if (failedPorts.size() == 1) {
112-
throw new PortInUseException(failedPorts.iterator().next().getNumber());
112+
throw new PortInUseException(failedPorts.iterator().next().getNumber(), ex);
113113
}
114114
}
115115
throw new WebServerException("Unable to start embedded Undertow", ex);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/PortInUseException.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,16 @@ public class PortInUseException extends WebServerException {
3232
* @param port the port that was in use
3333
*/
3434
public PortInUseException(int port) {
35-
super("Port " + port + " is already in use", null);
35+
this(port, null);
36+
}
37+
38+
/**
39+
* Creates a new port in use exception for the given {@code port}.
40+
* @param port the port that was in use
41+
* @param cause the cause of the exception
42+
*/
43+
public PortInUseException(int port, Throwable cause) {
44+
super("Port " + port + " is already in use", cause);
3645
this.port = port;
3746
}
3847

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/NettyReactiveWebServerFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void exceptionIsThrownWhenPortIsAlreadyInUse() {
6262
this.webServer.start();
6363
factory.setPort(this.webServer.getPort());
6464
assertThatExceptionOfType(PortInUseException.class).isThrownBy(factory.getWebServer(new EchoHandler())::start)
65-
.satisfies(this::portMatchesRequirement);
65+
.satisfies(this::portMatchesRequirement).withCauseInstanceOf(Throwable.class);
6666
}
6767

6868
private void portMatchesRequirement(PortInUseException exception) {

0 commit comments

Comments
 (0)