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
This commit is contained in:
parent
85befdf10e
commit
c7611112f7
|
|
@ -148,7 +148,7 @@ public class JettyWebServer implements WebServer {
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
if (connector instanceof NetworkConnector && findBindException(ex) != null) {
|
if (connector instanceof NetworkConnector && findBindException(ex) != null) {
|
||||||
throw new PortInUseException(((NetworkConnector) connector).getPort());
|
throw new PortInUseException(((NetworkConnector) connector).getPort(), ex);
|
||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -69,7 +69,7 @@ public class NettyWebServer implements WebServer {
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
ChannelBindException bindException = findBindException(ex);
|
ChannelBindException bindException = findBindException(ex);
|
||||||
if (bindException != null) {
|
if (bindException != null) {
|
||||||
throw new PortInUseException(bindException.localPort());
|
throw new PortInUseException(bindException.localPort(), ex);
|
||||||
}
|
}
|
||||||
throw new WebServerException("Unable to start Netty", ex);
|
throw new WebServerException("Unable to start Netty", ex);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -151,7 +151,7 @@ public class UndertowServletWebServer implements WebServer {
|
||||||
List<Port> actualPorts = getActualPorts();
|
List<Port> actualPorts = getActualPorts();
|
||||||
failedPorts.removeAll(actualPorts);
|
failedPorts.removeAll(actualPorts);
|
||||||
if (failedPorts.size() == 1) {
|
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);
|
throw new WebServerException("Unable to start embedded Undertow", ex);
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -109,7 +109,7 @@ public class UndertowWebServer implements WebServer {
|
||||||
List<UndertowWebServer.Port> actualPorts = getActualPorts();
|
List<UndertowWebServer.Port> actualPorts = getActualPorts();
|
||||||
failedPorts.removeAll(actualPorts);
|
failedPorts.removeAll(actualPorts);
|
||||||
if (failedPorts.size() == 1) {
|
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);
|
throw new WebServerException("Unable to start embedded Undertow", ex);
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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
|
* @param port the port that was in use
|
||||||
*/
|
*/
|
||||||
public PortInUseException(int port) {
|
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;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ public class NettyReactiveWebServerFactoryTests extends AbstractReactiveWebServe
|
||||||
this.webServer.start();
|
this.webServer.start();
|
||||||
factory.setPort(this.webServer.getPort());
|
factory.setPort(this.webServer.getPort());
|
||||||
assertThatExceptionOfType(PortInUseException.class).isThrownBy(factory.getWebServer(new EchoHandler())::start)
|
assertThatExceptionOfType(PortInUseException.class).isThrownBy(factory.getWebServer(new EchoHandler())::start)
|
||||||
.satisfies(this::portMatchesRequirement);
|
.satisfies(this::portMatchesRequirement).withCauseInstanceOf(Throwable.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void portMatchesRequirement(PortInUseException exception) {
|
private void portMatchesRequirement(PortInUseException exception) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue