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);
}
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);
}
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);
}
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();
}
}
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);
}
Aggregations