FebsGatewayExceptionHandler.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package cc.mrbird.febs.gateway.handler;
  2. import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.boot.autoconfigure.web.ErrorProperties;
  6. import org.springframework.boot.autoconfigure.web.ResourceProperties;
  7. import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
  8. import org.springframework.boot.web.reactive.error.ErrorAttributes;
  9. import org.springframework.cloud.gateway.support.NotFoundException;
  10. import org.springframework.cloud.gateway.support.TimeoutException;
  11. import org.springframework.context.ApplicationContext;
  12. import org.springframework.http.HttpStatus;
  13. import org.springframework.web.reactive.function.server.*;
  14. import org.springframework.web.server.ResponseStatusException;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. /**
  18. * @author MrBird
  19. */
  20. @Slf4j
  21. public class FebsGatewayExceptionHandler extends DefaultErrorWebExceptionHandler {
  22. public FebsGatewayExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties,
  23. ErrorProperties errorProperties, ApplicationContext applicationContext) {
  24. super(errorAttributes, resourceProperties, errorProperties, applicationContext);
  25. }
  26. /**
  27. * 异常处理,定义返回报文格式
  28. */
  29. @Override
  30. protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
  31. Throwable error = super.getError(request);
  32. log.error(
  33. "请求发生异常,请求URI:{},请求方法:{},异常信息:{}",
  34. request.path(), request.methodName(), error.getMessage()
  35. );
  36. String errorMessage;
  37. if (error instanceof NotFoundException) {
  38. String serverId = StringUtils.substringAfterLast(error.getMessage(), "Unable to find instance for ");
  39. serverId = StringUtils.replace(serverId, "\"", StringUtils.EMPTY);
  40. errorMessage = String.format("无法找到%s服务", serverId);
  41. } else if (StringUtils.containsIgnoreCase(error.getMessage(), "connection refused")) {
  42. errorMessage = "目标服务拒绝连接";
  43. } else if (error instanceof TimeoutException) {
  44. errorMessage = "访问服务超时";
  45. } else if (error instanceof ResponseStatusException
  46. && StringUtils.containsIgnoreCase(error.getMessage(), HttpStatus.NOT_FOUND.toString())) {
  47. errorMessage = "未找到该资源";
  48. } else if (error instanceof ParamFlowException) {
  49. errorMessage = "访问频率超限";
  50. } else {
  51. errorMessage = "网关转发异常";
  52. }
  53. Map<String, Object> errorAttributes = new HashMap<>(3);
  54. errorAttributes.put("message", errorMessage);
  55. return errorAttributes;
  56. }
  57. @Override
  58. @SuppressWarnings("all")
  59. protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
  60. return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
  61. }
  62. @Override
  63. protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) {
  64. return HttpStatus.INTERNAL_SERVER_ERROR;
  65. }
  66. }