Search in sources :

Example 1 with BadRequestException

use of info.xiancloud.nettyhttpserver.http.bean.BadRequestException in project xian by happyyangyuan.

the class DefaultExceptionHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    LOG.error("Exception caught", cause);
    HttpResponseStatus status = (cause instanceof BadRequestException) ? BAD_REQUEST : INTERNAL_SERVER_ERROR;
    String content = StringUtil.getExceptionStacktrace(cause);
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8));
    response.headers().set(CONTENT_TYPE, Config.getContentType());
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    /*ctx.writeAndFlush(response); todo 爆了异常之后,这里根本写不进去,直接查日志吧!*/
    ctx.close();
    LOG.warn("TODO: 16/9/10 目前是出现了异常则直接关闭,这样对长连接稳定性好像不太有利");
    MsgIdHolder.clear();
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BadRequestException(info.xiancloud.nettyhttpserver.http.bean.BadRequestException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 2 with BadRequestException

use of info.xiancloud.nettyhttpserver.http.bean.BadRequestException in project xian by happyyangyuan.

the class RequestDecoderAux method decode.

@Override
protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception {
    LOG.debug("    httpRequest  ---->   UnitRequest Pojo");
    /*if (!HttpMethod.POST.equals(msg.method())) {
            throw new BadRequestException(new IllegalArgumentException("拒绝非POST请求!"));
        }*/
    DecoderResult result = msg.decoderResult();
    if (!result.isSuccess()) {
        throw new BadRequestException(result.cause());
    }
    updateLongConnectionStatus(msg, ctx);
    Request request = new Request(msg, MsgIdHolder.get());
    offerReqQueue(ctx, request);
    out.add(request);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) Request(info.xiancloud.nettyhttpserver.http.bean.Request) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DecoderResult(io.netty.handler.codec.DecoderResult) BadRequestException(info.xiancloud.nettyhttpserver.http.bean.BadRequestException)

Aggregations

BadRequestException (info.xiancloud.nettyhttpserver.http.bean.BadRequestException)2 Request (info.xiancloud.nettyhttpserver.http.bean.Request)1 DecoderResult (io.netty.handler.codec.DecoderResult)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1