Fix NPE in Websocket logging (#202)

This commit is contained in:
Ryan Butler authored and GitHub committed 2022-06-28 20:21:38 +03:00
1 parent 7a4d087be2
commit 408df5273d
2 files changed
+24 -17

No files matched your search

@@ -176,21 +176,6 @@ public class WebSocketVRBridge extends WebsocketAPI implements Bridge {
}
}
@Override
public void onError(WebSocket conn, Exception ex) {
LogManager
.severe(
"[WebSocket] Exception on connection "
+ (conn != null
? conn
.getRemoteSocketAddress()
.getAddress()
.getHostAddress()
: null),
ex
);
}
@Override
public void onStart() {
LogManager.info("[WebSocket] Web Socket VR Bridge started on port " + getPort());
@@ -39,12 +39,30 @@ public class WebsocketAPI extends WebSocketServer implements ProtocolAPIServer {
conn.setAttachment(new WebsocketConnection(conn));
}
/**
* Helper function to get the string of the `conn` while handling `null`
*/
protected static String connAddr(WebSocket conn) {
if (conn == null) {
return "null";
}
var remote = conn.getRemoteSocketAddress();
if (remote == null) {
return conn.toString();
}
var addr = remote.getAddress();
if (addr == null) {
return remote.toString();
}
return addr.getHostAddress();
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
LogManager
.info(
"[WebSocketAPI] Disconnected: "
+ conn.getRemoteSocketAddress().getAddress().getHostAddress()
+ connAddr(conn)
+ ", ("
+ code
+ ") "
@@ -67,7 +85,11 @@ public class WebsocketAPI extends WebSocketServer implements ProtocolAPIServer {
@Override
public void onError(WebSocket conn, Exception ex) {
ex.printStackTrace();
LogManager
.severe(
"[WebSocket] Exception on connection " + connAddr(conn),
ex
);
}
@Override