Java Code Examples for com.netflix.zuul.context.RequestContext#getThrowable()

The following examples show how to use com.netflix.zuul.context.RequestContext#getThrowable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZuulErrorController.java    From open-capacity-platform with Apache License 2.0 5 votes vote down vote up
/**
 * 错误最终会到这里来
 */
@RequestMapping(ERROR_PATH)
@ResponseBody
public Object error() {
    RequestContext ctx = RequestContext.getCurrentContext();
    Throwable throwable = ctx.getThrowable();
    return Result.failed(throwable.getMessage()) ;
}
 
Example 2
Source File: ZuulErrorFilter.java    From open-cloud with MIT License 5 votes vote down vote up
@Override
public Object run() {
    // 代理错误日志记录
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    HttpServletResponse response = ctx.getResponse();
    Exception exception = new Exception(ctx.getThrowable());
    if(StringUtils.toBoolean(ctx.get("rateLimitExceeded"))){
        exception = new Exception(HttpStatus.TOO_MANY_REQUESTS.name());
    }
    accessLogService.sendLog(request, response,exception);
    ResultBody resultBody = OpenGlobalExceptionHandler.resolveException(exception,request.getRequestURI());
    WebUtils.writeJson(response, resultBody);
    return null;
}
 
Example 3
Source File: SentinelZuulErrorFilter.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() throws ZuulException {
    RequestContext ctx = RequestContext.getCurrentContext();
    Throwable throwable = ctx.getThrowable();
    if (throwable != null) {
        if (!BlockException.isBlockException(throwable)) {
            // Trace exception for each entry and exit entries in order.
            // The entries can be retrieved from the request context.
            SentinelEntryUtils.tryTraceExceptionThenExitFromCurrentContext(throwable);
            RecordLog.info("[SentinelZuulErrorFilter] Trace error cause", throwable.getCause());
        }
    }
    return null;
}
 
Example 4
Source File: ErrorFilter.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() {
    RequestContext ctx=RequestContext.getCurrentContext();
    Throwable throwable = ctx.getThrowable();
    ctx.setSendZuulResponse(false); //不对其进行路由
    ctx.setResponseStatusCode(401);
    HttpServletResponse response = ctx.getResponse();
    response.setHeader("content-type", "text/html;charset=utf8");
    ctx.setResponseBody("认证失败"+throwable.getCause().getMessage());
    ctx.set("code", 500);
    log.error("异常信息,{}",throwable.getCause().getMessage());
    return null;
}
 
Example 5
Source File: TracePostZuulFilter.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() {
  final RequestContext context = RequestContext.getCurrentContext();
  final Object spanObject = context.get(TracePreZuulFilter.CONTEXT_SPAN_KEY);
  if (spanObject instanceof Span) {
    final Span span = (Span)spanObject;
    span.setTag(Tags.HTTP_STATUS.getKey(), context.getResponseStatusCode());
    if (context.getThrowable() != null) {
      onError(context.getThrowable(), span);
    }
    else {
      final Object error = context.get("error.exception");
      if (error instanceof Exception)
        onError((Exception)error, span);
    }

    if (context.getRouteHost() != null)
      span.setTag(ROUTE_HOST_TAG, context.getRouteHost().toString());

    final Object scopeObject = context.get(TracePreZuulFilter.CONTEXT_SCOPE_KEY);
    if (scopeObject instanceof Scope)
      ((Scope)scopeObject).close();

    span.finish();
    context.remove(TracePreZuulFilter.CONTEXT_SPAN_KEY);
  }

  return null;
}
 
Example 6
Source File: HandlerController.java    From roncoo-education with MIT License 5 votes vote down vote up
@RequestMapping("/error")
@ResponseStatus(HttpStatus.OK)
public Result<String> error() {
	RequestContext ctx = RequestContext.getCurrentContext();
	Throwable throwable = ctx.getThrowable();
	if(null != throwable && throwable instanceof  ZuulException ){
		ZuulException e = (ZuulException) ctx.getThrowable();
		return Result.error(e.nStatusCode, e.errorCause);
	}
	return Result.error(ResultEnum.ERROR);
}
 
Example 7
Source File: ErrorExtFilter.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    Throwable throwable = ctx.getThrowable();
    logger.error("全局异常处理过滤器 ErrorFilter : {}", throwable.getCause().getMessage());
    ctx.setSendZuulResponse(false);// 对该请求不路由
    ctx.set("isSuccess", false);// 设值,让下一个Filter看到上一个Filter的状态
    dingTalkMessage(ctx, throwable);
    // 构建返回信息
    RetData retData = new RetData();
    retData.setCode(ResultCodeMessage.FAILURE);
    retData.setMessage(ResultCodeMessage.EXCEPTION_MESSAGE);
    retData.setData(new Object());
    return retData;
}
 
Example 8
Source File: SentinelZuulErrorFilter.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() throws ZuulException {
    RequestContext ctx = RequestContext.getCurrentContext();
    Throwable throwable = ctx.getThrowable();
    if (throwable != null) {
        if (!BlockException.isBlockException(throwable)) {
            // Trace exception for each entry and exit entries in order.
            // The entries can be retrieved from the request context.
            SentinelEntryUtils.tryTraceExceptionThenExitFromCurrentContext(throwable);
            RecordLog.info("[SentinelZuulErrorFilter] Trace error cause", throwable.getCause());
        }
    }
    return null;
}
 
Example 9
Source File: TracePostZuulFilter.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() {
  RequestContext ctx = RequestContext.getCurrentContext();

  Object spanObject = ctx.get(TracePreZuulFilter.CONTEXT_SPAN_KEY);
  if (spanObject instanceof Span) {
    Span span = (Span) spanObject;
    span.setTag(Tags.HTTP_STATUS.getKey(), ctx.getResponseStatusCode());

    if (ctx.getThrowable() != null) {
      onError(ctx.getThrowable(), span);
    } else {
      Object error = ctx.get("error.exception");
      if (error instanceof Exception) {
        onError((Exception) error, span);
      }
    }

    if (ctx.getRouteHost() != null) {
      span.setTag(ROUTE_HOST_TAG, ctx.getRouteHost().toString());
    }

    span.finish();
  }

  return null;
}
 
Example 10
Source File: SentinelZuulErrorFilter.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Override
public boolean shouldFilter() {
    RequestContext ctx = RequestContext.getCurrentContext();
    return ctx.getThrowable() != null;
}
 
Example 11
Source File: ErrorHandlerFilter.java    From pig with MIT License 4 votes vote down vote up
@Override
public boolean shouldFilter() {
    RequestContext requestContext = RequestContext.getCurrentContext();
    return requestContext.getThrowable() != null;
}
 
Example 12
Source File: ErrorHandlerFilter.java    From fw-cloud-framework with MIT License 4 votes vote down vote up
@Override
public boolean shouldFilter() {
	RequestContext requestContext = RequestContext.getCurrentContext();
	return requestContext.getThrowable() != null;
}
 
Example 13
Source File: SentinelZuulErrorFilter.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Override
public boolean shouldFilter() {
    RequestContext ctx = RequestContext.getCurrentContext();
    return ctx.getThrowable() != null;
}
 
Example 14
Source File: CustomErrorZuulFilter.java    From api-gateway-old with Apache License 2.0 4 votes vote down vote up
@Override
public boolean shouldFilter() {
    RequestContext ctx = RequestContext.getCurrentContext();
    return ctx.getThrowable() != null
            && !ctx.getBoolean(SEND_ERROR_FILTER_RAN, false);
}