diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java index 22b0c87c1141..23f2c83cebca 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClientHandler.java @@ -80,7 +80,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { super.write(ctx, msg, promise); final NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler); - final boolean isMsg = msg instanceof Request; + final boolean isRequest = msg instanceof Request; // We add listeners to make sure our out bound event is correct. // If our out bound event has an error (in most cases the encoder fails), @@ -97,11 +97,9 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) } Throwable t = future.cause(); - if (t != null && isMsg) { + if (t != null && isRequest) { Request request = (Request) msg; - Response response = new Response(request.getId(), request.getVersion()); - response.setStatus(Response.BAD_REQUEST); - response.setErrorMessage(StringUtils.toString(t)); + Response response = buildErrorResponse(request, t); handler.received(channel, response); } }); @@ -117,4 +115,18 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) NettyChannel.removeChannelIfDisconnected(ctx.channel()); } } + + /** + * build a bad request's response + * + * @param request the request + * @param t the throwable. In most cases, serialization fails. + * @return the response + */ + private static Response buildErrorResponse(Request request, Throwable t) { + Response response = new Response(request.getId(), request.getVersion()); + response.setStatus(Response.BAD_REQUEST); + response.setErrorMessage(StringUtils.toString(t)); + return response; + } } \ No newline at end of file