Search in sources :

Example 11 with FlowException

use of com.alibaba.csp.sentinel.slots.block.flow.FlowException in project flash-sale by ThoughtsBeta.

the class InternalExceptionHandler method handleConflict.

@ExceptionHandler(value = { Exception.class, RuntimeException.class })
protected ResponseEntity<Object> handleConflict(Exception ex, WebRequest request) {
    ExceptionResponse exceptionResponse = new ExceptionResponse();
    if (ex instanceof UndeclaredThrowableException) {
        if (((UndeclaredThrowableException) ex).getUndeclaredThrowable() instanceof FlowException) {
            exceptionResponse.setErrorCode(LIMIT_BLOCK.getCode());
            exceptionResponse.setErrorMessage(LIMIT_BLOCK.getDesc());
        }
    } else {
        exceptionResponse.setErrorCode(INTERNAL_ERROR.getCode());
        exceptionResponse.setErrorMessage(INTERNAL_ERROR.getDesc());
    }
    logger.error("unknownError|未知错误|{}", ex.getClass());
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    return handleExceptionInternal(ex, JSON.toJSONString(exceptionResponse), httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 12 with FlowException

use of com.alibaba.csp.sentinel.slots.block.flow.FlowException in project amusing-project by m-lvqingyu.

the class GatewayBlockExceptionHandler method handle.

@Override
public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable) {
    ServerHttpResponse response = serverWebExchange.getResponse();
    setProps(response);
    ServerHttpRequest request = serverWebExchange.getRequest();
    String path = request.getURI().getPath();
    // 限流
    if (throwable instanceof FlowException) {
        log.error("[gateway-sentinel]-request is flow, path:{}", path);
        return responseWrite(response, ApiResult.result(CommCode.FLOW_ERROR));
    }
    // 熔断
    if (throwable instanceof DegradeException) {
        log.error("[gateway-sentinel]-request is degrade, path:{}", path);
        return responseWrite(response, ApiResult.result(CommCode.DEGRADE_ERROR));
    }
    // 热点参数限流
    if (throwable instanceof ParamFlowException) {
        log.error("[gateway-sentinel]-request is paramFlow, path:{}", path);
        return responseWrite(response, ApiResult.result(CommCode.PARAM_FLOW_ERROR));
    }
    // 系统保护规则
    if (throwable instanceof SystemBlockException) {
        log.error("[gateway-sentinel]-request is systemBlock, path:{}", path);
        return responseWrite(response, ApiResult.result(CommCode.SYSTEM_BLOCK_ERROR));
    }
    // 授权规则
    if (throwable instanceof AuthorityException) {
        log.error("[gateway-sentinel]-request is authority, path:{}", path);
        return responseWrite(response, ApiResult.result(CommCode.AUTHORITY_ERROR));
    }
    return responseWrite(response, ApiResult.result(CommCode.FREQUENT_OPERATION_EXCEPTION));
}
Also used : FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) SystemBlockException(com.alibaba.csp.sentinel.slots.system.SystemBlockException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) AuthorityException(com.alibaba.csp.sentinel.slots.block.authority.AuthorityException)

Aggregations

FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)12 Test (org.junit.Test)7 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)4 DegradeException (com.alibaba.csp.sentinel.slots.block.degrade.DegradeException)3 HttpHeaders (org.springframework.http.HttpHeaders)3 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)3 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)3 AuthorityException (com.alibaba.csp.sentinel.slots.block.authority.AuthorityException)2 ParamFlowException (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException)2 SystemBlockException (com.alibaba.csp.sentinel.slots.system.SystemBlockException)2 Result (com.alibaba.dubbo.rpc.Result)2 RpcResult (com.alibaba.dubbo.rpc.RpcResult)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 BizException (com.actionworks.flashsale.app.exception.BizException)1 DomainException (com.actionworks.flashsale.domain.exception.DomainException)1 Context (com.alibaba.csp.sentinel.context.Context)1 StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)1 Invocation (com.alibaba.dubbo.rpc.Invocation)1 PrintWriter (java.io.PrintWriter)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1