Search in sources :

Example 6 with FlowException

use of com.alibaba.csp.sentinel.slots.block.flow.FlowException in project Sentinel by alibaba.

the class ZuulBlockFallbackProviderTest method testFlowControlFallbackResponse.

@Test
public void testFlowControlFallbackResponse() throws Exception {
    ZuulBlockFallbackProvider fallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(ALL_ROUTE);
    BlockResponse clientHttpResponse = fallbackProvider.fallbackResponse(ALL_ROUTE, new FlowException("flow exception"));
    Assert.assertEquals(clientHttpResponse.getCode(), 429);
}
Also used : FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) Test(org.junit.Test)

Example 7 with FlowException

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

the class BadRequestExceptionHandler method handleConflict.

@ExceptionHandler(value = { BizException.class, FlowException.class, AuthException.class, DomainException.class })
protected ResponseEntity<Object> handleConflict(RuntimeException 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 if (ex instanceof BizException || ex instanceof DomainException) {
        exceptionResponse.setErrorCode(BIZ_ERROR.getCode());
        exceptionResponse.setErrorMessage(ex.getMessage());
    } else if (ex instanceof AuthException) {
        exceptionResponse.setErrorCode(AUTH_ERROR.getCode());
        exceptionResponse.setErrorMessage(AUTH_ERROR.getDesc());
    }
    logger.error("expectedException|预期错误|{},{}", ex.getMessage(), ex);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    return handleExceptionInternal(ex, JSON.toJSONString(exceptionResponse), httpHeaders, HttpStatus.BAD_REQUEST, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) DomainException(com.actionworks.flashsale.domain.exception.DomainException) FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) BizException(com.actionworks.flashsale.app.exception.BizException) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 8 with FlowException

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

the class SentinelExceptionHandler method handleConflict.

@ExceptionHandler(value = { UndeclaredThrowableException.class })
protected ResponseEntity<Object> handleConflict(UndeclaredThrowableException ex, WebRequest request) {
    ExceptionResponse exceptionResponse = new ExceptionResponse();
    if (ex.getUndeclaredThrowable() instanceof FlowException) {
        exceptionResponse.setErrorCode(LIMIT_BLOCK.getCode());
        exceptionResponse.setErrorMessage(LIMIT_BLOCK.getDesc());
    }
    if (ex.getUndeclaredThrowable() instanceof DegradeException) {
        exceptionResponse.setErrorCode(DEGRADE_BLOCK.getCode());
        exceptionResponse.setErrorMessage(DEGRADE_BLOCK.getDesc());
    }
    logger.error("expectedException|预期错误|{},{}", ex.getMessage(), ex);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    return handleExceptionInternal(ex, JSON.toJSONString(exceptionResponse), httpHeaders, HttpStatus.BAD_REQUEST, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) DegradeException(com.alibaba.csp.sentinel.slots.block.degrade.DegradeException) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 9 with FlowException

use of com.alibaba.csp.sentinel.slots.block.flow.FlowException in project java-demos by powerLeePlus.

the class CustomUrlBlockHandler method handle.

@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws Exception {
    String msg = null;
    if (ex instanceof FlowException) {
        msg = "限流了";
    } else if (ex instanceof DegradeException) {
        msg = "降级了";
    } else if (ex instanceof ParamFlowException) {
        msg = "热点参数限流";
    } else if (ex instanceof SystemBlockException) {
        msg = "系统规则(负载/...不满足要求)";
    } else if (ex instanceof AuthorityException) {
        msg = "授权规则不通过";
    }
    // http状态码
    response.setStatus(500);
    response.setCharacterEncoding("utf-8");
    response.setHeader("Content-Type", "application/json;charset=utf-8");
    response.setContentType("application/json;charset=utf-8");
    try (PrintWriter writer = response.getWriter()) {
        writer.write(msg);
        writer.flush();
    }
}
Also used : FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException) 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) AuthorityException(com.alibaba.csp.sentinel.slots.block.authority.AuthorityException) PrintWriter(java.io.PrintWriter)

Example 10 with FlowException

use of com.alibaba.csp.sentinel.slots.block.flow.FlowException in project Sentinel by alibaba.

the class MetricEntryCallbackTest method onBlocked.

@Test
public void onBlocked() throws Exception {
    FakeMetricExtension extension = new FakeMetricExtension();
    FakeAdvancedMetricExtension advancedExtension = new FakeAdvancedMetricExtension();
    MetricExtensionProvider.addMetricExtension(extension);
    MetricExtensionProvider.addMetricExtension(advancedExtension);
    MetricEntryCallback entryCallback = new MetricEntryCallback();
    StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
    Context context = mock(Context.class);
    when(context.getOrigin()).thenReturn("origin1");
    int count = 2;
    Object[] args = { "args1", "args2" };
    entryCallback.onBlocked(new FlowException("xx"), context, resourceWrapper, null, count, args);
    // assert extension
    Assert.assertEquals(extension.block, count);
    // assert advancedExtension
    Assert.assertEquals(advancedExtension.block, count);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Context(com.alibaba.csp.sentinel.context.Context) FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) Test(org.junit.Test)

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