|
@@ -1,14 +1,23 @@
|
|
|
package com.winhc.fast.query.configuration;
|
|
|
|
|
|
-import com.alibaba.lindorm.client.exception.NotServingRegionException;
|
|
|
+import com.github.jaemon.dinger.DingerSender;
|
|
|
+import com.github.jaemon.dinger.core.entity.DingerRequest;
|
|
|
+import com.github.jaemon.dinger.core.entity.enums.MessageSubType;
|
|
|
import com.winhc.fast.query.vo.ResponseVo;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.hadoop.hbase.client.RetriesExhaustedException;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.io.StringWriter;
|
|
|
+
|
|
|
+import static com.winhc.tool.utils.BaseUtils.isWindows;
|
|
|
+
|
|
|
/**
|
|
|
* @author: XuJiakai
|
|
|
* 2022/5/25 15:38
|
|
@@ -18,22 +27,74 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
@AllArgsConstructor
|
|
|
public class GlobalExceptionHandler {
|
|
|
private HbaseHealthIndicator hbaseHealthIndicator;
|
|
|
+ private DingerSender dingerSender;
|
|
|
+
|
|
|
+ public static String getErrorMsg(Throwable thr) {
|
|
|
+ StringWriter stringWriter = new StringWriter();
|
|
|
+ PrintWriter printWriter = new PrintWriter(stringWriter);
|
|
|
+ try {
|
|
|
+ thr.printStackTrace(printWriter);
|
|
|
+ return stringWriter.toString();
|
|
|
+ } finally {
|
|
|
+ if (printWriter != null) {
|
|
|
+ printWriter.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
@ResponseStatus(HttpStatus.OK)
|
|
|
- @ExceptionHandler(value = Exception.class)
|
|
|
- public ResponseVo handleBadRequest(Exception e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
+ @ExceptionHandler(value = RetriesExhaustedException.class)
|
|
|
+ public ResponseVo handleHbaseException(RetriesExhaustedException e) {
|
|
|
+ String message = e.getMessage();
|
|
|
+ if (StringUtils.isNotBlank(message) && message.contains("TableNotFoundException")) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ } else {
|
|
|
+// checkHbaseError(e);
|
|
|
+ error(true, e);
|
|
|
+ log.error("exception name:" + e.getClass().getName());
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
return ResponseVo.failure(e.getMessage());
|
|
|
}
|
|
|
|
|
|
@ResponseStatus(HttpStatus.OK)
|
|
|
- @ExceptionHandler(value = NotServingRegionException.class)
|
|
|
- public ResponseVo handleBadRequest(NotServingRegionException e) {
|
|
|
- log.error("发现hbase异常!");
|
|
|
+ @ExceptionHandler(value = Exception.class)
|
|
|
+ public ResponseVo handleBadRequest(Exception e) {
|
|
|
+// checkHbaseError(e);
|
|
|
+ error(true, e);
|
|
|
+ log.error("exception name:" + e.getClass().getName());
|
|
|
log.error(e.getMessage(), e);
|
|
|
- hbaseHealthIndicator.putErrorInfo(e);
|
|
|
return ResponseVo.failure(e.getMessage());
|
|
|
}
|
|
|
|
|
|
|
|
|
+ private void error(boolean isRestart, Exception e) {
|
|
|
+ Boolean windows = isWindows();
|
|
|
+ if (windows) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String name = e.getClass().getName();
|
|
|
+ if (!name.startsWith("org.apache.hadoop.hbase")) {
|
|
|
+ log.warn("error: {}", name);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String errorMsg = getErrorMsg(e);
|
|
|
+ dingerSender.send(
|
|
|
+ MessageSubType.MARKDOWN,
|
|
|
+ DingerRequest.request("exception name: " + e.getClass().getName() + "\n" +
|
|
|
+ "\n" +
|
|
|
+ "exception msg:" + e.getMessage() + "\n" +
|
|
|
+ "\n" +
|
|
|
+ "```\n" +
|
|
|
+ errorMsg +
|
|
|
+ "\n" +
|
|
|
+ "```\n", isRestart ? "触发重启" : "程序不健康")
|
|
|
+ );
|
|
|
+ if (isRestart) {
|
|
|
+ hbaseHealthIndicator.putErrorInfo(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|