diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java index d174f2fc56ac..ad98984b7ec1 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java @@ -148,7 +148,7 @@ public void start() throws WebServerException { } catch (IOException ex) { if (connector instanceof NetworkConnector && findBindException(ex) != null) { - throw new PortInUseException(((NetworkConnector) connector).getPort()); + throw new PortInUseException(((NetworkConnector) connector).getPort(), ex); } throw ex; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java index 36f86ac90528..0f23b94b13ab 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -69,7 +69,7 @@ public void start() throws WebServerException { catch (Exception ex) { ChannelBindException bindException = findBindException(ex); if (bindException != null) { - throw new PortInUseException(bindException.localPort()); + throw new PortInUseException(bindException.localPort(), ex); } throw new WebServerException("Unable to start Netty", ex); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java index 4003d0f5ece0..45ea5954f89a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -151,7 +151,7 @@ public void start() throws WebServerException { List actualPorts = getActualPorts(); failedPorts.removeAll(actualPorts); if (failedPorts.size() == 1) { - throw new PortInUseException(failedPorts.iterator().next().getNumber()); + throw new PortInUseException(failedPorts.iterator().next().getNumber(), ex); } } throw new WebServerException("Unable to start embedded Undertow", ex); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java index 6f099dbdb6b3..bb6f10d8daaf 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -109,7 +109,7 @@ public void start() throws WebServerException { List actualPorts = getActualPorts(); failedPorts.removeAll(actualPorts); if (failedPorts.size() == 1) { - throw new PortInUseException(failedPorts.iterator().next().getNumber()); + throw new PortInUseException(failedPorts.iterator().next().getNumber(), ex); } } throw new WebServerException("Unable to start embedded Undertow", ex); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/PortInUseException.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/PortInUseException.java index 2b7e0bcfa44f..edfb21c2a52a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/PortInUseException.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/PortInUseException.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,16 @@ public class PortInUseException extends WebServerException { * @param port the port that was in use */ public PortInUseException(int port) { - super("Port " + port + " is already in use", null); + this(port, null); + } + + /** + * Creates a new port in use exception for the given {@code port}. + * @param port the port that was in use + * @param cause the cause of the exception + */ + public PortInUseException(int port, Throwable cause) { + super("Port " + port + " is already in use", cause); this.port = port; } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/NettyReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/NettyReactiveWebServerFactoryTests.java index c455ae901bc8..09f0eec85a08 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/NettyReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/NettyReactiveWebServerFactoryTests.java @@ -62,7 +62,7 @@ public void exceptionIsThrownWhenPortIsAlreadyInUse() { this.webServer.start(); factory.setPort(this.webServer.getPort()); assertThatExceptionOfType(PortInUseException.class).isThrownBy(factory.getWebServer(new EchoHandler())::start) - .satisfies(this::portMatchesRequirement); + .satisfies(this::portMatchesRequirement).withCauseInstanceOf(Throwable.class); } private void portMatchesRequirement(PortInUseException exception) {