|
@@ -2,6 +2,7 @@ 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.MonitorDetectException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.dao.DataAccessException;
|
|
import org.springframework.dao.DataAccessException;
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
@@ -12,6 +13,8 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
|
|
|
|
|
+import static com.usthe.common.util.CommonConstants.DETECT_FAILED;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* controller exception handler
|
|
* controller exception handler
|
|
|
* @author tomsun28
|
|
* @author tomsun28
|
|
@@ -22,13 +25,25 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
public class GlobalExceptionHandler {
|
|
public class GlobalExceptionHandler {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * 处理探测失败
|
|
|
|
|
+ * @param exception 探测异常
|
|
|
|
|
+ * @return response
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(MonitorDetectException.class)
|
|
|
|
|
+ @ResponseBody
|
|
|
|
|
+ ResponseEntity<Message<Void>> handleMonitorDetectException(MonitorDetectException exception) {
|
|
|
|
|
+ Message<Void> message = Message.<Void>builder().msg(exception.getMessage()).code(DETECT_FAILED).build();
|
|
|
|
|
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
* handler the exception thrown for data input verify
|
|
* handler the exception thrown for data input verify
|
|
|
* @param exception data input verify exception
|
|
* @param exception data input verify exception
|
|
|
* @return response
|
|
* @return response
|
|
|
*/
|
|
*/
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
@ResponseBody
|
|
@ResponseBody
|
|
|
- ResponseEntity<Message> handleInputValidException(MethodArgumentNotValidException exception) {
|
|
|
|
|
|
|
+ ResponseEntity<Message<Void>> handleInputValidException(MethodArgumentNotValidException exception) {
|
|
|
StringBuffer errorMessage = new StringBuffer();
|
|
StringBuffer errorMessage = new StringBuffer();
|
|
|
if (exception != null) {
|
|
if (exception != null) {
|
|
|
exception.getBindingResult().getAllErrors().forEach(error ->
|
|
exception.getBindingResult().getAllErrors().forEach(error ->
|
|
@@ -37,7 +52,7 @@ public class GlobalExceptionHandler {
|
|
|
if (log.isDebugEnabled()) {
|
|
if (log.isDebugEnabled()) {
|
|
|
log.debug("[sample-tom]-[input argument not valid happen]-{}", errorMessage, exception);
|
|
log.debug("[sample-tom]-[input argument not valid happen]-{}", errorMessage, exception);
|
|
|
}
|
|
}
|
|
|
- Message message = Message.builder().msg(errorMessage.toString()).build();
|
|
|
|
|
|
|
+ Message<Void> message = Message.<Void>builder().msg(errorMessage.toString()).build();
|
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -48,13 +63,13 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
*/
|
|
|
@ExceptionHandler(DataAccessException.class)
|
|
@ExceptionHandler(DataAccessException.class)
|
|
|
@ResponseBody
|
|
@ResponseBody
|
|
|
- ResponseEntity<Message> handleDataAccessException(DataAccessException exception) {
|
|
|
|
|
|
|
+ ResponseEntity<Message<Void>> handleDataAccessException(DataAccessException exception) {
|
|
|
String errorMessage = "database error happen";
|
|
String errorMessage = "database error happen";
|
|
|
if (exception != null) {
|
|
if (exception != null) {
|
|
|
errorMessage = exception.getMessage();
|
|
errorMessage = exception.getMessage();
|
|
|
}
|
|
}
|
|
|
log.warn("[sample-tom]-[database error happen]-{}", errorMessage, exception);
|
|
log.warn("[sample-tom]-[database error happen]-{}", errorMessage, exception);
|
|
|
- Message message = Message.builder().msg(errorMessage).build();
|
|
|
|
|
|
|
+ Message<Void> message = Message.<Void>builder().msg(errorMessage).build();
|
|
|
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
|
|
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
|
|
|
}
|
|
}
|
|
|
|
|
|