Repository: netty/netty
Issue: Websocket example and WebSocketHandshakeException
Labels: defect

Hi,

Websocket example contains the following code:

        if (!handshaker.isHandshakeComplete()) {
            handshaker.finishHandshake(ch, (FullHttpResponse) msg);
            System.out.println("WebSocket Client connected!");
            handshakeFuture.setSuccess();
            return;
        }

(https://github.com/netty/netty/blob/4.1/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java#L85)

The problem I've encountered is that `finishHandshake` may throw `WebSocketHandshakeException` exception. It would be nice to handle the exception. Possibly like this:

        try {
            handshaker.finishHandshake(ch, (FullHttpResponse) msg);
            handshakeFuture.setSuccess();
        } catch (WebSocketHandshakeException e) {
            handshakeFuture.setFailure(e);
        }

I'm quite new to Netty so I don't know there is a better way. Does the modification make sense to you? 

### Netty version

4.1.13


Regards,
Martin

Issue comments:
@MartyIX yes... Can you please open a PR with a fix ?
Yes, https://github.com/netty/netty/pull/6999
Fixes by https://github.com/netty/netty/pull/6999 . @MartyIX thanks

Pull request review comments:
![MAJOR](https://raw.githubusercontent.com/SonarCommunity/sonar-github/master/images/severity-major.png) Replace this usage of System.out or System.err by a logger. [![rule](https://raw.githubusercontent.com/SonarCommunity/sonar-github/master/images/rule.png)](https://garage.netty.io/sonarqube/coding_rules#rule_key=squid%3AS106)

![MAJOR](https://raw.githubusercontent.com/SonarCommunity/sonar-github/master/images/severity-major.png) Replace this usage of System.out or System.err by a logger. [![rule](https://raw.githubusercontent.com/SonarCommunity/sonar-github/master/images/rule.png)](https://garage.netty.io/sonarqube/coding_rules#rule_key=squid%3AS106)

@normanmaurer Is it necessary to replace it? The code is an example. It's easier to grab code with println and run it somewhere without configuring of a logger..
I think its fine as we also use println for the success case
@normanmaurer OK. So what should I do next to get this pull request merged?
nothing... I will merge soon.

Patch that resolved the issue:
diff --git a/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java b/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java
index c36283cd8ef..fb10576e2e4 100644
--- a/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java
+++ b/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java
@@ -48,6 +48,7 @@
 import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
 import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
 import io.netty.handler.codec.http.websocketx.WebSocketFrame;
+import io.netty.handler.codec.http.websocketx.WebSocketHandshakeException;
 import io.netty.util.CharsetUtil;
 
 public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
@@ -82,9 +83,14 @@ public void channelInactive(ChannelHandlerContext ctx) {
     public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
         Channel ch = ctx.channel();
         if (!handshaker.isHandshakeComplete()) {
-            handshaker.finishHandshake(ch, (FullHttpResponse) msg);
-            System.out.println("WebSocket Client connected!");
-            handshakeFuture.setSuccess();
+            try {
+                handshaker.finishHandshake(ch, (FullHttpResponse) msg);
+                System.out.println("WebSocket Client connected!");
+                handshakeFuture.setSuccess();
+            } catch (WebSocketHandshakeException e) {
+                System.out.println("WebSocket Client failed to connect");
+                handshakeFuture.setFailure(e);
+            }
             return;
         }
 


Test patch: