[manager]完善全局异常拦截
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
package com.usthe.manager.support;
|
package com.usthe.manager.support;
|
||||||
|
|
||||||
|
|
||||||
import com.usthe.common.entity.dto.Message;
|
import com.usthe.common.entity.dto.Message;
|
||||||
import com.usthe.manager.support.exception.MonitorDatabaseException;
|
import com.usthe.manager.support.exception.MonitorDatabaseException;
|
||||||
import com.usthe.manager.support.exception.MonitorDetectException;
|
import com.usthe.manager.support.exception.MonitorDetectException;
|
||||||
@@ -32,6 +31,8 @@ import static com.usthe.common.util.CommonConstants.PARAM_INVALID_CODE;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
private static final String CONNECT_STR = "||";
|
||||||
|
|
||||||
private static Field detailMessage;
|
private static Field detailMessage;
|
||||||
|
|
||||||
private static Field fieldErrorField;
|
private static Field fieldErrorField;
|
||||||
@@ -66,7 +67,7 @@ public class GlobalExceptionHandler {
|
|||||||
@ResponseBody
|
@ResponseBody
|
||||||
ResponseEntity<Message<Void>> handleMonitorDatabaseException(MonitorDatabaseException exception) {
|
ResponseEntity<Message<Void>> handleMonitorDatabaseException(MonitorDatabaseException exception) {
|
||||||
Message<Void> message = Message.<Void>builder().msg(exception.getMessage()).code(MONITOR_CONFLICT_CODE).build();
|
Message<Void> message = Message.<Void>builder().msg(exception.getMessage()).code(MONITOR_CONFLICT_CODE).build();
|
||||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
|
return ResponseEntity.ok(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -78,7 +79,7 @@ public class GlobalExceptionHandler {
|
|||||||
@ResponseBody
|
@ResponseBody
|
||||||
ResponseEntity<Message<Void>> handleIllegalArgumentException(IllegalArgumentException exception) {
|
ResponseEntity<Message<Void>> handleIllegalArgumentException(IllegalArgumentException exception) {
|
||||||
Message<Void> message = Message.<Void>builder().msg(exception.getMessage()).code(PARAM_INVALID_CODE).build();
|
Message<Void> message = Message.<Void>builder().msg(exception.getMessage()).code(PARAM_INVALID_CODE).build();
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
|
return ResponseEntity.ok(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,24 +115,28 @@ public class GlobalExceptionHandler {
|
|||||||
exception.getBindingResult().getAllErrors().forEach(error -> {
|
exception.getBindingResult().getAllErrors().forEach(error -> {
|
||||||
try {
|
try {
|
||||||
String field = (String) fieldErrorField.get(error);
|
String field = (String) fieldErrorField.get(error);
|
||||||
errorMessage.append(field).append(":").append(error.getDefaultMessage()).append("||");
|
errorMessage.append(field).append(":").append(error.getDefaultMessage()).append(CONNECT_STR);
|
||||||
} catch (Exception e1) {
|
} catch (Exception e1) {
|
||||||
errorMessage.append(error.getDefaultMessage()).append("||");
|
errorMessage.append(error.getDefaultMessage()).append(CONNECT_STR);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (e instanceof BindException) {
|
} else if (e instanceof BindException) {
|
||||||
BindException exception = (BindException)e;
|
BindException exception = (BindException)e;
|
||||||
exception.getAllErrors().forEach(error -> {
|
exception.getAllErrors().forEach(error ->
|
||||||
errorMessage.append(error.getDefaultMessage()).append("||");
|
errorMessage.append(error.getDefaultMessage()).append(CONNECT_STR));
|
||||||
});
|
}
|
||||||
|
String errorMsg = errorMessage.toString();
|
||||||
|
if (errorMsg.endsWith(CONNECT_STR)) {
|
||||||
|
errorMsg = errorMsg.substring(0, errorMsg.length() - 2);
|
||||||
}
|
}
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("[input argument not valid happen]-{}", errorMessage, e);
|
log.debug("[input argument not valid happen]-{}", errorMsg, e);
|
||||||
}
|
}
|
||||||
Message<Void> message = Message.<Void>builder().msg(errorMessage.toString()).code(PARAM_INVALID_CODE).build();
|
Message<Void> message = Message.<Void>builder().msg(errorMsg).code(PARAM_INVALID_CODE).build();
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* handler the exception thrown for datastore error
|
* handler the exception thrown for datastore error
|
||||||
* @param exception datastore exception
|
* @param exception datastore exception
|
||||||
|
|||||||
Reference in New Issue
Block a user