Java Code Examples for com.alibaba.csp.sentinel.slots.block.BlockException#isBlockException()

The following examples show how to use com.alibaba.csp.sentinel.slots.block.BlockException#isBlockException() . 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: 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 2
Source File: SentinelGatewayBlockExceptionHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
    if (exchange.getResponse().isCommitted()) {
        return Mono.error(ex);
    }
    // This exception handler only handles rejection by Sentinel.
    if (!BlockException.isBlockException(ex)) {
        return Mono.error(ex);
    }
    return handleBlockedRequest(exchange, ex)
        .flatMap(response -> writeResponse(response, exchange));
}
 
Example 3
Source File: SentinelBlockExceptionHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
    if (exchange.getResponse().isCommitted()) {
        return Mono.error(ex);
    }
    // This exception handler only handles rejection by Sentinel.
    if (!BlockException.isBlockException(ex)) {
        return Mono.error(ex);
    }
    return handleBlockedRequest(exchange, ex)
        .flatMap(response -> writeResponse(response, exchange));
}
 
Example 4
Source File: ClusterNode.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
/**
 * Add exception count only when given {@code throwable} is not a {@link BlockException}.
 *
 * @param throwable target exception
 * @param count     count to add
 */
public void trace(Throwable throwable, int count) {
    if (count <= 0) {
        return;
    }
    if (!BlockException.isBlockException(throwable)) {
        this.increaseExceptionQps(count);
    }
}
 
Example 5
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 6
Source File: SentinelGatewayBlockExceptionHandler.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
    if (exchange.getResponse().isCommitted()) {
        return Mono.error(ex);
    }
    // This exception handler only handles rejection by Sentinel.
    if (!BlockException.isBlockException(ex)) {
        return Mono.error(ex);
    }
    return handleBlockedRequest(exchange, ex)
        .flatMap(response -> writeResponse(response, exchange));
}
 
Example 7
Source File: SentinelBlockExceptionHandler.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
    if (exchange.getResponse().isCommitted()) {
        return Mono.error(ex);
    }
    // This exception handler only handles rejection by Sentinel.
    if (!BlockException.isBlockException(ex)) {
        return Mono.error(ex);
    }
    return handleBlockedRequest(exchange, ex)
        .flatMap(response -> writeResponse(response, exchange));
}
 
Example 8
Source File: SentinelProtectInterceptor.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
		ClientHttpRequestExecution execution) throws IOException {
	URI uri = request.getURI();
	String hostResource = request.getMethod().toString() + ":" + uri.getScheme()
			+ "://" + uri.getHost()
			+ (uri.getPort() == -1 ? "" : ":" + uri.getPort());
	String hostWithPathResource = hostResource + uri.getPath();
	boolean entryWithPath = true;
	if (hostResource.equals(hostWithPathResource)) {
		entryWithPath = false;
	}
	Method urlCleanerMethod = BlockClassRegistry.lookupUrlCleaner(
			sentinelRestTemplate.urlCleanerClass(),
			sentinelRestTemplate.urlCleaner());
	if (urlCleanerMethod != null) {
		hostWithPathResource = (String) methodInvoke(urlCleanerMethod,
				hostWithPathResource);
	}

	Entry hostEntry = null;
	Entry hostWithPathEntry = null;
	ClientHttpResponse response = null;
	try {
		hostEntry = SphU.entry(hostResource, EntryType.OUT);
		if (entryWithPath) {
			hostWithPathEntry = SphU.entry(hostWithPathResource, EntryType.OUT);
		}
		response = execution.execute(request, body);
		if (this.restTemplate.getErrorHandler().hasError(response)) {
			Tracer.trace(
					new IllegalStateException("RestTemplate ErrorHandler has error"));
		}
	}
	catch (Throwable e) {
		if (!BlockException.isBlockException(e)) {
			Tracer.trace(e);
		}
		else {
			return handleBlockException(request, body, execution, (BlockException) e);
		}
	}
	finally {
		if (hostWithPathEntry != null) {
			hostWithPathEntry.exit();
		}
		if (hostEntry != null) {
			hostEntry.exit();
		}
	}
	return response;
}