|
|
@@ -2,8 +2,6 @@
|
|
|
*/
|
|
|
package com.uas.search.console.b2b.core.advice;
|
|
|
|
|
|
-import java.sql.SQLRecoverableException;
|
|
|
-
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
@@ -17,94 +15,41 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import com.uas.search.b2b.exception.SearchException;
|
|
|
|
|
|
/**
|
|
|
- * <p>
|
|
|
* 基于Application的异常处理,以AOP的形式注册到SpringMVC的处理链
|
|
|
- * </p>
|
|
|
- * <p>
|
|
|
- * 正常的业务流程,只需抛出对应的异常和相关的信息
|
|
|
- * </P>
|
|
|
- * <p>
|
|
|
- * 不同的错误,对应不同的方法来处理
|
|
|
- * </p>
|
|
|
- * <p>
|
|
|
- * 可以用不同的HttpStatus来表示具体的异常类型
|
|
|
- * </p>
|
|
|
- * <p>
|
|
|
- * 客户端可以基于对应的HttpStatus Code做出最有利于自己的处理
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @author yingp
|
|
|
*
|
|
|
+ * @author sunyj
|
|
|
+ * @since 2017年7月11日 下午4:58:01
|
|
|
*/
|
|
|
@ControllerAdvice
|
|
|
public class ExceptionHandlerAdvice {
|
|
|
|
|
|
- private final static Logger logger = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理运行时抛出异常
|
|
|
- *
|
|
|
- * @param ex
|
|
|
- * @return
|
|
|
- */
|
|
|
- @ExceptionHandler(RuntimeException.class)
|
|
|
- public ResponseEntity<String> handleUnexpectedServerError(RuntimeException ex) {
|
|
|
- logger.error("RuntimeException", ex);
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.add("Content-Type", "application/text; charset=utf-8");
|
|
|
- return new ResponseEntity<String>("\u7CFB\u7EDF\u9519\u8BEF", headers, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理连接池的连接失效抛出异常
|
|
|
- *
|
|
|
- * @see SQLRecoverableException
|
|
|
- * @param ex
|
|
|
- * @return
|
|
|
- */
|
|
|
- @ExceptionHandler(SQLRecoverableException.class)
|
|
|
- public ResponseEntity<String> handleSQLRecoverableException(SQLRecoverableException ex) {
|
|
|
- logger.error("SQLRecoverableException", ex);
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.add("Content-Type", "application/text; charset=utf-8");
|
|
|
- return new ResponseEntity<String>("\u7CFB\u7EDF\u9519\u8BEF", headers, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理参数错误抛出异常
|
|
|
- *
|
|
|
- * @see IllegalArgumentException
|
|
|
- * @param ex
|
|
|
- * @return
|
|
|
- */
|
|
|
- @ExceptionHandler(IllegalArgumentException.class)
|
|
|
- public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException ex) {
|
|
|
- logger.error("IllegalArgumentException", ex);
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.add("Content-Type", "application/text; charset=utf-8");
|
|
|
- String message = !StringUtils.isEmpty(ex.getMessage()) ? ex.getMessage() : "\u53C2\u6570\u9519\u8BEF";
|
|
|
- return new ResponseEntity<String>(message, headers, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
- }
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);
|
|
|
|
|
|
/**
|
|
|
* 处理已捕获异常,明确传达给客户端错误码、错误信息
|
|
|
*
|
|
|
- * @see SearchException
|
|
|
- * @param ex
|
|
|
+ * @param e
|
|
|
* @return
|
|
|
*/
|
|
|
- @ExceptionHandler(SearchException.class)
|
|
|
- public ResponseEntity<ModelMap> handleSearchException(SearchException ex) {
|
|
|
- if (!StringUtils.isEmpty(ex.getDetailedMessage())) {
|
|
|
- logger.error(ex.getDetailedMessage());
|
|
|
+ @ExceptionHandler(Throwable.class)
|
|
|
+ public ResponseEntity<ModelMap> handleEdiException(Throwable e) {
|
|
|
+ logger.error("", e);
|
|
|
+ ModelMap map = new ModelMap();
|
|
|
+ map.put("success", false);
|
|
|
+ if (e instanceof SearchException) {
|
|
|
+ map.put("message", e.getMessage());
|
|
|
+ } else {
|
|
|
+ map.put("message", e.getClass().getSimpleName() + ": " + e.getMessage());
|
|
|
}
|
|
|
- logger.error(ex.getMessage(), ex);
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.add("Content-Type", "application/json; charset=utf-8");
|
|
|
- ModelMap map = new ModelMap();
|
|
|
- map.put("success", false);
|
|
|
- map.put("message", ex.getMessage());
|
|
|
- return new ResponseEntity<ModelMap>(map, headers, HttpStatus.BAD_REQUEST);
|
|
|
+ if (e instanceof SearchException) {
|
|
|
+ SearchException exception = (SearchException) e;
|
|
|
+ if (!StringUtils.isEmpty(exception.getDetailedMessage())) {
|
|
|
+ map.put("detailedMessage", exception.getDetailedMessage());
|
|
|
+ }
|
|
|
+ return new ResponseEntity<ModelMap>(map, headers, HttpStatus.BAD_REQUEST);
|
|
|
+ }
|
|
|
+ return new ResponseEntity<ModelMap>(map, headers, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
}
|
|
|
-
|
|
|
-}
|
|
|
+}
|