Skip to content

Commit

Permalink
Fix with the reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
carryxyh committed Jan 8, 2019
1 parent ae53c5c commit 540c7a4
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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);
}
});
Expand All @@ -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;
}
}

0 comments on commit 540c7a4

Please sign in to comment.