Search in sources :

Example 1 with ZuulException

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

the class FilterProcessor method processZuulFilter.

/**
     * Processes an individual ZuulFilter. This method adds Debug information. Any uncaught Thowables are caught by this method and converted to a ZuulException with a 500 status code.
     *
     * @param filter
     * @return the return value for that filter
     * @throws ZuulException
     */
public Object processZuulFilter(ZuulFilter filter) throws ZuulException {
    RequestContext ctx = RequestContext.getCurrentContext();
    boolean bDebug = ctx.debugRouting();
    final String metricPrefix = "zuul.filter-";
    long execTime = 0;
    String filterName = "";
    try {
        long ltime = System.currentTimeMillis();
        filterName = filter.getClass().getSimpleName();
        RequestContext copy = null;
        Object o = null;
        Throwable t = null;
        if (bDebug) {
            Debug.addRoutingDebug("Filter " + filter.filterType() + " " + filter.filterOrder() + " " + filterName);
            copy = ctx.copy();
        }
        ZuulFilterResult result = filter.runFilter();
        ExecutionStatus s = result.getStatus();
        execTime = System.currentTimeMillis() - ltime;
        switch(s) {
            case FAILED:
                t = result.getException();
                ctx.addFilterExecutionSummary(filterName, ExecutionStatus.FAILED.name(), execTime);
                break;
            case SUCCESS:
                o = result.getResult();
                ctx.addFilterExecutionSummary(filterName, ExecutionStatus.SUCCESS.name(), execTime);
                if (bDebug) {
                    Debug.addRoutingDebug("Filter {" + filterName + " TYPE:" + filter.filterType() + " ORDER:" + filter.filterOrder() + "} Execution time = " + execTime + "ms");
                    Debug.compareContextState(filterName, copy);
                }
                break;
            default:
                break;
        }
        if (t != null)
            throw t;
        usageNotifier.notify(filter, s);
        return o;
    } catch (Throwable e) {
        if (bDebug) {
            Debug.addRoutingDebug("Running Filter failed " + filterName + " type:" + filter.filterType() + " order:" + filter.filterOrder() + " " + e.getMessage());
        }
        usageNotifier.notify(filter, ExecutionStatus.FAILED);
        if (e instanceof ZuulException) {
            throw (ZuulException) e;
        } else {
            ZuulException ex = new ZuulException(e, "Filter threw Exception", 500, filter.filterType() + ":" + filterName);
            ctx.addFilterExecutionSummary(filterName, ExecutionStatus.FAILED.name(), execTime);
            throw ex;
        }
    }
}
Also used : ZuulException(com.netflix.zuul.exception.ZuulException) RequestContext(com.netflix.zuul.context.RequestContext)

Example 2 with ZuulException

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

the class SendErrorFilterTests method createSendErrorFilter.

private SendErrorFilter createSendErrorFilter(HttpServletRequest request) {
    RequestContext context = new RequestContext();
    context.setRequest(request);
    context.setResponse(new MockHttpServletResponse());
    context.setThrowable(new ZuulException(new RuntimeException(), HttpStatus.NOT_FOUND.value(), null));
    RequestContext.testSetCurrentContext(context);
    SendErrorFilter filter = new SendErrorFilter();
    filter.setErrorPath("/error");
    return filter;
}
Also used : ZuulException(com.netflix.zuul.exception.ZuulException) RequestContext(com.netflix.zuul.context.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 3 with ZuulException

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

the class RibbonRoutingFilter method run.

@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();
    this.helper.addIgnoredHeaders();
    try {
        RibbonCommandContext commandContext = buildCommandContext(context);
        ClientHttpResponse response = forward(commandContext);
        setResponse(response);
        return response;
    } catch (ZuulException ex) {
        throw new ZuulRuntimeException(ex);
    } catch (Exception ex) {
        throw new ZuulRuntimeException(ex);
    }
}
Also used : RibbonCommandContext(org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext) ZuulRuntimeException(org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException) ZuulException(com.netflix.zuul.exception.ZuulException) RequestContext(com.netflix.zuul.context.RequestContext) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) ClientException(com.netflix.client.ClientException) ZuulRuntimeException(org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException) IOException(java.io.IOException) ZuulException(com.netflix.zuul.exception.ZuulException)

Example 4 with ZuulException

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

the class SendErrorFilter method run.

@Override
public Object run() {
    try {
        RequestContext ctx = RequestContext.getCurrentContext();
        ZuulException exception = findZuulException(ctx.getThrowable());
        HttpServletRequest request = ctx.getRequest();
        request.setAttribute("javax.servlet.error.status_code", exception.nStatusCode);
        log.warn("Error during filtering", exception);
        request.setAttribute("javax.servlet.error.exception", exception);
        if (StringUtils.hasText(exception.errorCause)) {
            request.setAttribute("javax.servlet.error.message", exception.errorCause);
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher(this.errorPath);
        if (dispatcher != null) {
            ctx.set(SEND_ERROR_FILTER_RAN, true);
            if (!ctx.getResponse().isCommitted()) {
                ctx.setResponseStatusCode(exception.nStatusCode);
                dispatcher.forward(request, ctx.getResponse());
            }
        }
    } catch (Exception ex) {
        ReflectionUtils.rethrowRuntimeException(ex);
    }
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ZuulException(com.netflix.zuul.exception.ZuulException) RequestContext(com.netflix.zuul.context.RequestContext) RequestDispatcher(javax.servlet.RequestDispatcher) ZuulRuntimeException(org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException) ZuulException(com.netflix.zuul.exception.ZuulException)

Example 5 with ZuulException

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

the class ClientRequestReceiver method handleExpect100Continue.

private void handleExpect100Continue(ChannelHandlerContext ctx, HttpRequest req) {
    if (HttpUtil.is100ContinueExpected(req)) {
        PerfMark.event("CRR.handleExpect100Continue");
        final ChannelFuture f = ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
        f.addListener((s) -> {
            if (!s.isSuccess()) {
                throw new ZuulException(s.cause(), "Failed while writing 100-continue response", true);
            }
        });
        // Remove the Expect: 100-Continue header from request as we don't want to proxy it downstream.
        req.headers().remove(HttpHeaderNames.EXPECT);
        zuulRequest.getHeaders().remove(HttpHeaderNames.EXPECT.toString());
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ZuulException(com.netflix.zuul.exception.ZuulException)

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