diff --git a/src/main/java/dan200/computercraft/core/apis/http/NetworkUtils.java b/src/main/java/dan200/computercraft/core/apis/http/NetworkUtils.java index aec3abace..f3804f847 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/NetworkUtils.java +++ b/src/main/java/dan200/computercraft/core/apis/http/NetworkUtils.java @@ -11,12 +11,19 @@ import dan200.computercraft.core.apis.http.options.Options; import dan200.computercraft.shared.util.ThreadUtils; import io.netty.buffer.ByteBuf; +import io.netty.channel.ConnectTimeoutException; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.handler.codec.DecoderException; +import io.netty.handler.codec.TooLongFrameException; +import io.netty.handler.codec.http.websocketx.WebSocketHandshakeException; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.timeout.ReadTimeoutException; +import javax.annotation.Nonnull; import javax.net.ssl.SSLException; +import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.TrustManagerFactory; import java.net.InetSocketAddress; import java.net.URI; @@ -161,4 +168,29 @@ public static byte[] toBytes( ByteBuf buffer ) buffer.readBytes( bytes ); return bytes; } + + @Nonnull + public static String toFriendlyError( @Nonnull Throwable cause ) + { + if( cause instanceof WebSocketHandshakeException || cause instanceof HTTPRequestException ) + { + return cause.getMessage(); + } + else if( cause instanceof TooLongFrameException ) + { + return "Message is too large"; + } + else if( cause instanceof ReadTimeoutException || cause instanceof ConnectTimeoutException ) + { + return "Timed out"; + } + else if( cause instanceof SSLHandshakeException || (cause instanceof DecoderException && cause.getCause() instanceof SSLHandshakeException) ) + { + return "Could not create a secure connection"; + } + else + { + return "Could not connect"; + } + } } diff --git a/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java b/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java index c6447b77b..2049575ff 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java +++ b/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequest.java @@ -19,13 +19,10 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; -import io.netty.channel.ConnectTimeoutException; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.http.*; import io.netty.handler.ssl.SslContext; -import io.netty.handler.timeout.ReadTimeoutException; import io.netty.handler.timeout.ReadTimeoutHandler; import java.net.InetSocketAddress; @@ -190,7 +187,7 @@ protected void initChannel( SocketChannel ch ) .remoteAddress( socketAddress ) .connect() .addListener( c -> { - if( !c.isSuccess() ) failure( c.cause() ); + if( !c.isSuccess() ) failure( NetworkUtils.toFriendlyError( c.cause() ) ); } ); // Do an additional check for cancellation @@ -202,7 +199,7 @@ protected void initChannel( SocketChannel ch ) } catch( Exception e ) { - failure( "Could not connect" ); + failure( NetworkUtils.toFriendlyError( e ) ); if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error in HTTP request", e ); } } @@ -212,29 +209,6 @@ void failure( String message ) if( tryClose() ) environment.queueEvent( FAILURE_EVENT, address, message ); } - void failure( Throwable cause ) - { - String message; - if( cause instanceof HTTPRequestException ) - { - message = cause.getMessage(); - } - else if( cause instanceof TooLongFrameException ) - { - message = "Response is too large"; - } - else if( cause instanceof ReadTimeoutException || cause instanceof ConnectTimeoutException ) - { - message = "Timed out"; - } - else - { - message = "Could not connect"; - } - - failure( message ); - } - void failure( String message, HttpResponseHandle object ) { if( tryClose() ) environment.queueEvent( FAILURE_EVENT, address, message, object ); diff --git a/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequestHandler.java b/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequestHandler.java index e4e6445b4..4ef42ff44 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequestHandler.java +++ b/src/main/java/dan200/computercraft/core/apis/http/request/HttpRequestHandler.java @@ -183,7 +183,7 @@ public void channelRead0( ChannelHandlerContext ctx, HttpObject message ) public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause ) { if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error handling HTTP response", cause ); - request.failure( cause ); + request.failure( NetworkUtils.toFriendlyError( cause ) ); } private void sendResponse() diff --git a/src/main/java/dan200/computercraft/core/apis/http/websocket/Websocket.java b/src/main/java/dan200/computercraft/core/apis/http/websocket/Websocket.java index 296e27eda..fa1399bb6 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/websocket/Websocket.java +++ b/src/main/java/dan200/computercraft/core/apis/http/websocket/Websocket.java @@ -166,7 +166,7 @@ protected void initChannel( SocketChannel ch ) .remoteAddress( socketAddress ) .connect() .addListener( c -> { - if( !c.isSuccess() ) failure( c.cause().getMessage() ); + if( !c.isSuccess() ) failure( NetworkUtils.toFriendlyError( c.cause() ) ); } ); // Do an additional check for cancellation @@ -178,7 +178,7 @@ protected void initChannel( SocketChannel ch ) } catch( Exception e ) { - failure( "Could not connect" ); + failure( NetworkUtils.toFriendlyError( e ) ); if( ComputerCraft.logComputerErrors ) ComputerCraft.log.error( "Error in websocket", e ); } } diff --git a/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandler.java b/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandler.java index 9cac47ce8..4916f5fbd 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandler.java +++ b/src/main/java/dan200/computercraft/core/apis/http/websocket/WebsocketHandler.java @@ -5,17 +5,13 @@ */ package dan200.computercraft.core.apis.http.websocket; -import dan200.computercraft.core.apis.http.HTTPRequestException; import dan200.computercraft.core.apis.http.NetworkUtils; import dan200.computercraft.core.apis.http.options.Options; import dan200.computercraft.core.tracking.TrackingField; import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ConnectTimeoutException; import io.netty.channel.SimpleChannelInboundHandler; -import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.websocketx.*; -import io.netty.handler.timeout.ReadTimeoutException; import io.netty.util.CharsetUtil; import static dan200.computercraft.core.apis.http.websocket.Websocket.MESSAGE_EVENT; @@ -97,24 +93,7 @@ public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause ) { ctx.close(); - String message; - if( cause instanceof WebSocketHandshakeException || cause instanceof HTTPRequestException ) - { - message = cause.getMessage(); - } - else if( cause instanceof TooLongFrameException ) - { - message = "Message is too large"; - } - else if( cause instanceof ReadTimeoutException || cause instanceof ConnectTimeoutException ) - { - message = "Timed out"; - } - else - { - message = "Could not connect"; - } - + String message = NetworkUtils.toFriendlyError( cause ); if( handshaker.isHandshakeComplete() ) { websocket.close( -1, message );