Search in sources :

Example 11 with ZuulException

use of com.netflix.zuul.exception.ZuulException in project zuul by Netflix.

the class OriginResponseReceiver method writeInternal.

private void writeInternal(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!ctx.channel().isActive()) {
        ReferenceCountUtil.release(msg);
        return;
    }
    if (msg instanceof HttpRequestMessage) {
        promise.addListener((future) -> {
            if (!future.isSuccess()) {
                Throwable cause = ctx.channel().attr(SSL_HANDSHAKE_UNSUCCESS_FROM_ORIGIN_THROWABLE).get();
                if (cause != null) {
                    // Set the specific SSL handshake error if the handlers have already caught them
                    ctx.channel().attr(SSL_HANDSHAKE_UNSUCCESS_FROM_ORIGIN_THROWABLE).set(null);
                    fireWriteError("request headers", cause, ctx);
                    LOG.debug("SSLException is overridden by SSLHandshakeException caught in handler level. Original SSL exception message: ", future.cause());
                } else {
                    fireWriteError("request headers", future.cause(), ctx);
                }
            }
        });
        HttpRequestMessage zuulReq = (HttpRequestMessage) msg;
        preWriteHook(ctx, zuulReq);
        super.write(ctx, buildOriginHttpRequest(zuulReq), promise);
    } else if (msg instanceof HttpContent) {
        promise.addListener((future) -> {
            if (!future.isSuccess()) {
                fireWriteError("request content chunk", future.cause(), ctx);
            }
        });
        super.write(ctx, msg, promise);
    } else {
        // should never happen
        ReferenceCountUtil.release(msg);
        throw new ZuulException("Received invalid message from client", true);
    }
}
Also used : AttributeKey(io.netty.util.AttributeKey) HttpVersion(io.netty.handler.codec.http.HttpVersion) LoggerFactory(org.slf4j.LoggerFactory) READ_TIMEOUT(com.netflix.zuul.exception.OutboundErrorType.READ_TIMEOUT) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) ChannelPromise(io.netty.channel.ChannelPromise) ReadTimeoutException(io.netty.handler.timeout.ReadTimeoutException) SslHandshakeCompletionEvent(io.netty.handler.ssl.SslHandshakeCompletionEvent) HttpQueryParams(com.netflix.zuul.message.http.HttpQueryParams) TaskCloseable(io.perfmark.TaskCloseable) ProxyEndpoint(com.netflix.zuul.filters.endpoint.ProxyEndpoint) Header(com.netflix.zuul.message.Header) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) RESET_CONNECTION(com.netflix.zuul.exception.OutboundErrorType.RESET_CONNECTION) CompleteEvent(com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent) HttpContent(io.netty.handler.codec.http.HttpContent) OriginConnectException(com.netflix.zuul.netty.connectionpool.OriginConnectException) HttpRequest(io.netty.handler.codec.http.HttpRequest) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) Logger(org.slf4j.Logger) OutboundException(com.netflix.zuul.exception.OutboundException) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) PerfMark(io.perfmark.PerfMark) ZuulException(com.netflix.zuul.exception.ZuulException) PassportState(com.netflix.zuul.passport.PassportState) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) HttpResponse(io.netty.handler.codec.http.HttpResponse) CompleteReason(com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason) ChannelUtils(com.netflix.zuul.netty.ChannelUtils) SESSION_COMPLETE(com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason.SESSION_COMPLETE) HttpRequestMessage(com.netflix.zuul.message.http.HttpRequestMessage) ZuulException(com.netflix.zuul.exception.ZuulException) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 12 with ZuulException

use of com.netflix.zuul.exception.ZuulException in project zuul by Netflix.

the class ProxyEndpoint method handleError.

private void handleError(final Throwable cause) {
    final ZuulException ze = (cause instanceof ZuulException) ? (ZuulException) cause : requestAttemptFactory.mapNettyToOutboundException(cause, context);
    LOG.debug("Proxy endpoint failed.", cause);
    if (!startedSendingResponseToClient) {
        startedSendingResponseToClient = true;
        zuulResponse = new HttpResponseMessageImpl(context, zuulRequest, ze.getStatusCode());
        // TODO - why close the connection? maybe don't always want this to happen ...
        zuulResponse.getHeaders().add("Connection", "close");
        zuulResponse.finishBufferedBodyIfIncomplete();
        invokeNext(zuulResponse);
    } else {
        channelCtx.fireExceptionCaught(ze);
    }
}
Also used : HttpResponseMessageImpl(com.netflix.zuul.message.http.HttpResponseMessageImpl) ZuulException(com.netflix.zuul.exception.ZuulException)

Example 13 with ZuulException

use of com.netflix.zuul.exception.ZuulException in project zuul by Netflix.

the class ZuulServlet method service.

@Override
public void service(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse) throws ServletException, IOException {
    try {
        init((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse);
        // Marks this request as having passed through the "Zuul engine", as opposed to servlets
        // explicitly bound in web.xml, for which requests will not have the same data attached
        RequestContext context = RequestContext.getCurrentContext();
        context.setZuulEngineRan();
        try {
            preRoute();
        } catch (ZuulException e) {
            error(e);
            postRoute();
            return;
        }
        try {
            route();
        } catch (ZuulException e) {
            error(e);
            postRoute();
            return;
        }
        try {
            postRoute();
        } catch (ZuulException e) {
            error(e);
            return;
        }
    } catch (Throwable e) {
        error(new ZuulException(e, 500, "UNHANDLED_EXCEPTION_" + e.getClass().getName()));
    } finally {
        RequestContext.getCurrentContext().unset();
    }
}
Also used : ZuulException(com.netflix.zuul.exception.ZuulException) RequestContext(com.netflix.zuul.context.RequestContext)

Example 14 with ZuulException

use of com.netflix.zuul.exception.ZuulException in project paascloud-master by paascloud.

the class AuthHeaderFilter method doSomething.

private void doSomething(RequestContext requestContext) throws ZuulException {
    HttpServletRequest request = requestContext.getRequest();
    String requestURI = request.getRequestURI();
    if (OPTIONS.equalsIgnoreCase(request.getMethod()) || !requestURI.contains(AUTH_PATH) || !requestURI.contains(LOGOUT_URI) || !requestURI.contains(ALIPAY_CALL_URI)) {
        return;
    }
    String authHeader = RequestUtil.getAuthHeader(request);
    if (PublicUtil.isEmpty(authHeader)) {
        throw new ZuulException("刷新页面重试", 403, "check token fail");
    }
    if (authHeader.startsWith(BEARER_TOKEN_TYPE)) {
        requestContext.addZuulRequestHeader(HttpHeaders.AUTHORIZATION, authHeader);
        log.info("authHeader={} ", authHeader);
        // 传递给后续微服务
        requestContext.addZuulRequestHeader(CoreHeaderInterceptor.HEADER_LABEL, authHeader);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ZuulException(com.netflix.zuul.exception.ZuulException)

Example 15 with ZuulException

use of com.netflix.zuul.exception.ZuulException in project spring-cloud-netflix by spring-cloud.

the class RibbonRoutingFilter method handleException.

protected ClientHttpResponse handleException(Map<String, Object> info, HystrixRuntimeException ex) throws ZuulException {
    int statusCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
    Throwable cause = ex;
    String message = ex.getFailureType().toString();
    ClientException clientException = findClientException(ex);
    if (clientException == null) {
        clientException = findClientException(ex.getFallbackException());
    }
    if (clientException != null) {
        if (clientException.getErrorType() == ClientException.ErrorType.SERVER_THROTTLED) {
            statusCode = HttpStatus.SERVICE_UNAVAILABLE.value();
        }
        cause = clientException;
        message = clientException.getErrorType().toString();
    }
    info.put("status", String.valueOf(statusCode));
    throw new ZuulException(cause, "Forwarding error", statusCode, message);
}
Also used : ZuulException(com.netflix.zuul.exception.ZuulException) ClientException(com.netflix.client.ClientException)

Aggregations

ZuulException (com.netflix.zuul.exception.ZuulException)22 RequestContext (com.netflix.zuul.context.RequestContext)5 IOException (java.io.IOException)4 CompleteEvent (com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent)3 CompleteReason (com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason)3 SessionContext (com.netflix.zuul.context.SessionContext)3 HttpContent (io.netty.handler.codec.http.HttpContent)3 ClientException (com.netflix.client.ClientException)2 SESSION_COMPLETE (com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason.SESSION_COMPLETE)2 OutboundException (com.netflix.zuul.exception.OutboundException)2 ProxyEndpoint (com.netflix.zuul.filters.endpoint.ProxyEndpoint)2 HttpQueryParams (com.netflix.zuul.message.http.HttpQueryParams)2 HttpRequestMessage (com.netflix.zuul.message.http.HttpRequestMessage)2 HttpResponseMessage (com.netflix.zuul.message.http.HttpResponseMessage)2 HttpResponseMessageImpl (com.netflix.zuul.message.http.HttpResponseMessageImpl)2 ChannelUtils (com.netflix.zuul.netty.ChannelUtils)2 PassportState (com.netflix.zuul.passport.PassportState)2 ZuulStatusCategory (com.netflix.zuul.stats.status.ZuulStatusCategory)2 Channel (io.netty.channel.Channel)2 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)2