Bladeren bron

move ExceptionHandlerAdvice

sunyj 7 jaren geleden
bovenliggende
commit
5fdfbd630e

+ 44 - 0
search-common/src/main/java/com/uas/search/util/ExceptionUtils.java

@@ -0,0 +1,44 @@
+package com.uas.search.util;
+
+/**
+ * 处理异常的工具类
+ * 
+ * @author sunyj
+ * @since 2017年8月10日 下午3:26:55
+ */
+public class ExceptionUtils {
+
+	/**
+	 * 获取异常及其Cause拼接成的字符串
+	 * 
+	 * @param e
+	 *            异常
+	 * @return 拼接后的结果
+	 */
+	public static String getMessage(Throwable e) {
+		StringBuilder sb = new StringBuilder(e.toString());
+		if (e.getCause() != null) {
+			sb.append("\nCaused by: ").append(getMessage(e.getCause()));
+		}
+		return sb.toString();
+	}
+
+	/**
+	 * 获取异常及其Cause的StackTrace拼接成的字符串
+	 * 
+	 * @param e
+	 *            异常
+	 * @return 拼接后的结果
+	 */
+	public static String getDetailedMessage(Throwable e) {
+		StringBuilder sb = new StringBuilder(e.toString());
+		StackTraceElement[] stackTraceElements = e.getStackTrace();
+		for (StackTraceElement stackTraceElement : stackTraceElements) {
+			sb.append("\n\t").append(stackTraceElement.toString());
+		}
+		if (e.getCause() != null) {
+			sb.append("\nCaused by: ").append(getDetailedMessage(e.getCause()));
+		}
+		return sb.toString();
+	}
+}

+ 42 - 54
search-console-b2b/src/main/java/com/uas/search/console/b2b/core/advice/ExceptionHandlerAdvice.java → search-console-b2b/src/main/java/com/uas/search/console/b2b/aop/ExceptionHandlerAdvice.java

@@ -1,55 +1,43 @@
-/*CopyRright (c)2014: <www.usoftchina.com>
- */
-package com.uas.search.console.b2b.core.advice;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.ui.ModelMap;
-import com.uas.search.util.StringUtils;
-import org.springframework.web.bind.annotation.ControllerAdvice;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-
-import com.uas.search.b2b.exception.SearchException;
-
-/**
- * 基于Application的异常处理,以AOP的形式注册到SpringMVC的处理链
- * 
- * @author sunyj
- * @since 2017年7月11日 下午4:58:01
- */
-@ControllerAdvice
-public class ExceptionHandlerAdvice {
-
-	private static Logger logger = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);
-
-	/**
-	 * 处理已捕获异常,明确传达给客户端错误码、错误信息
-	 * 
-	 * @param e
-	 * @return
-	 */
-	@ExceptionHandler(Throwable.class)
-	public ResponseEntity<ModelMap> handleException(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());
-		}
-		HttpHeaders headers = new HttpHeaders();
-		headers.add("Content-Type", "application/json; charset=utf-8");
-		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);
-	}
+/*CopyRright (c)2014: <www.usoftchina.com>
+ */
+package com.uas.search.console.b2b.aop;
+
+import com.uas.search.util.ExceptionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.ui.ModelMap;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+
+/**
+ * 基于Application的异常处理,以AOP的形式注册到SpringMVC的处理链
+ * 
+ * @author sunyj
+ * @since 2017年7月11日 下午4:58:01
+ */
+@ControllerAdvice
+public class ExceptionHandlerAdvice {
+
+	private static Logger logger = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);
+
+	/**
+	 * 处理已捕获异常,明确传达给客户端错误码、错误信息
+	 *
+	 * @param e
+	 * @return
+	 */
+	@ExceptionHandler(Throwable.class)
+	public ResponseEntity<ModelMap> handleError(Throwable e) {
+		logger.error("", e);
+		ModelMap map = new ModelMap();
+		map.put("success", false);
+		map.put("message", e.getMessage());
+		map.put("detailedMessage", ExceptionUtils.getDetailedMessage(e));
+		HttpHeaders headers = new HttpHeaders();
+		headers.add("Content-Type", "application/json; charset=utf-8");
+		return new ResponseEntity<ModelMap>(map, headers, HttpStatus.INTERNAL_SERVER_ERROR);
+	}
 }

+ 77 - 77
search-console-b2b/src/main/java/com/uas/search/console/b2b/core/util/ContextUtils.java → search-console-b2b/src/main/java/com/uas/search/console/b2b/util/ContextUtils.java

@@ -1,77 +1,77 @@
-package com.uas.search.console.b2b.core.util;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.boot.context.event.ApplicationPreparedEvent;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.ApplicationEvent;
-import org.springframework.context.ApplicationListener;
-
-/**
- * spring容器上下文对象
- * 
- * @author yingp
- *
- */
-public class ContextUtils implements ApplicationListener<ApplicationPreparedEvent> {
-
-	private static ApplicationContext applicationContext;
-
-	private static Logger logger = LoggerFactory.getLogger(ContextUtils.class);
-
-	@Override
-	public void onApplicationEvent(ApplicationPreparedEvent event) {
-		synchronized (ContextUtils.class) {
-			logger.debug("setApplicationContext, notifyAll");
-			ContextUtils.applicationContext = event.getApplicationContext();
-			ContextUtils.class.notifyAll();
-		}
-	}
-
-	public static ApplicationContext getApplicationContext() {
-		synchronized (ContextUtils.class) {
-			while (applicationContext == null) {
-				try {
-					logger.debug("getApplicationContext, wait...");
-					ContextUtils.class.wait(60000);
-					if (applicationContext == null) {
-						logger.warn("Have been waiting for ApplicationContext to be set for 1 minute", new Exception());
-					}
-				} catch (InterruptedException ex) {
-					logger.debug("getApplicationContext, wait interrupted");
-				}
-			}
-			return applicationContext;
-		}
-	}
-
-	/**
-	 * 获取bean
-	 * 
-	 * @param name
-	 * @return
-	 */
-	public static Object getBean(String name) {
-		return getApplicationContext().getBean(name);
-	}
-
-	/**
-	 * 获取bean
-	 * 
-	 * @param cls
-	 * @return
-	 */
-	public static <T> T getBean(Class<T> cls) {
-		return getApplicationContext().getBean(cls);
-	}
-
-	/**
-	 * 触发事件
-	 * 
-	 * @param event
-	 */
-	public static void publishEvent(ApplicationEvent event) {
-		getApplicationContext().publishEvent(event);
-	}
-
-}
+package com.uas.search.console.b2b.util;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.context.event.ApplicationPreparedEvent;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationEvent;
+import org.springframework.context.ApplicationListener;
+
+/**
+ * spring容器上下文对象
+ * 
+ * @author yingp
+ *
+ */
+public class ContextUtils implements ApplicationListener<ApplicationPreparedEvent> {
+
+	private static ApplicationContext applicationContext;
+
+	private static Logger logger = LoggerFactory.getLogger(ContextUtils.class);
+
+	@Override
+	public void onApplicationEvent(ApplicationPreparedEvent event) {
+		synchronized (ContextUtils.class) {
+			logger.debug("setApplicationContext, notifyAll");
+			ContextUtils.applicationContext = event.getApplicationContext();
+			ContextUtils.class.notifyAll();
+		}
+	}
+
+	public static ApplicationContext getApplicationContext() {
+		synchronized (ContextUtils.class) {
+			while (applicationContext == null) {
+				try {
+					logger.debug("getApplicationContext, wait...");
+					ContextUtils.class.wait(60000);
+					if (applicationContext == null) {
+						logger.warn("Have been waiting for ApplicationContext to be set for 1 minute", new Exception());
+					}
+				} catch (InterruptedException ex) {
+					logger.debug("getApplicationContext, wait interrupted");
+				}
+			}
+			return applicationContext;
+		}
+	}
+
+	/**
+	 * 获取bean
+	 * 
+	 * @param name
+	 * @return
+	 */
+	public static Object getBean(String name) {
+		return getApplicationContext().getBean(name);
+	}
+
+	/**
+	 * 获取bean
+	 * 
+	 * @param cls
+	 * @return
+	 */
+	public static <T> T getBean(Class<T> cls) {
+		return getApplicationContext().getBean(cls);
+	}
+
+	/**
+	 * 触发事件
+	 * 
+	 * @param event
+	 */
+	public static void publishEvent(ApplicationEvent event) {
+		getApplicationContext().publishEvent(event);
+	}
+
+}

+ 83 - 83
search-console-b2b/src/main/java/com/uas/search/console/b2b/core/util/PathUtils.java → search-console-b2b/src/main/java/com/uas/search/console/b2b/util/PathUtils.java

@@ -1,83 +1,83 @@
-package com.uas.search.console.b2b.core.util;
-
-import java.io.File;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * 路径
- * 
- * @author yingp
- *
- */
-public class PathUtils {
-
-	private static Logger logger = LoggerFactory.getLogger(PathUtils.class);
-
-	private static String classPath;
-
-	private static String appPath;
-
-	private static String filePath;
-
-	/**
-	 * classes文件目录
-	 * 
-	 * @return
-	 */
-	public static String getClassPath() {
-		if (classPath == null)
-			setClassPath();
-		return classPath;
-	}
-
-	/**
-	 * 应用程序目录
-	 * 
-	 * @return
-	 */
-	public static String getAppPath() {
-		if (appPath == null)
-			setAppPath();
-		return appPath;
-	}
-
-	/**
-	 * 日志、附件文件等存放目录,与程序同级
-	 * 
-	 * @return
-	 */
-	public static String getFilePath() {
-		if (filePath == null)
-			setFilePath();
-		return filePath;
-	}
-
-	private static void setClassPath() {
-		Class<?> objClass = ContextUtils.getApplicationContext().getClass();
-		String strRealPath = objClass.getClassLoader().getResource("").getFile();
-		try {
-			strRealPath = URLDecoder.decode(strRealPath, "UTF-8");
-		} catch (UnsupportedEncodingException e) {
-			logger.error("", e);
-		}
-		File objFile = new File(strRealPath);
-		classPath = objFile.getParent() + File.separator;
-		if (classPath.contains("/")) {
-			classPath = "/" + classPath;
-		}
-	}
-
-	private static void setAppPath() {
-		File file = new File(getClassPath());
-		appPath = file.getParent() + File.separator;
-	}
-
-	private static void setFilePath() {
-		File file = new File(getAppPath());
-		filePath = file.getParent() + File.separator;
-	}
-}
+package com.uas.search.console.b2b.util;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+
+/**
+ * 路径
+ * 
+ * @author yingp
+ *
+ */
+public class PathUtils {
+
+	private static Logger logger = LoggerFactory.getLogger(PathUtils.class);
+
+	private static String classPath;
+
+	private static String appPath;
+
+	private static String filePath;
+
+	/**
+	 * classes文件目录
+	 * 
+	 * @return
+	 */
+	public static String getClassPath() {
+		if (classPath == null)
+			setClassPath();
+		return classPath;
+	}
+
+	/**
+	 * 应用程序目录
+	 * 
+	 * @return
+	 */
+	public static String getAppPath() {
+		if (appPath == null)
+			setAppPath();
+		return appPath;
+	}
+
+	/**
+	 * 日志、附件文件等存放目录,与程序同级
+	 * 
+	 * @return
+	 */
+	public static String getFilePath() {
+		if (filePath == null)
+			setFilePath();
+		return filePath;
+	}
+
+	private static void setClassPath() {
+		Class<?> objClass = ContextUtils.getApplicationContext().getClass();
+		String strRealPath = objClass.getClassLoader().getResource("").getFile();
+		try {
+			strRealPath = URLDecoder.decode(strRealPath, "UTF-8");
+		} catch (UnsupportedEncodingException e) {
+			logger.error("", e);
+		}
+		File objFile = new File(strRealPath);
+		classPath = objFile.getParent() + File.separator;
+		if (classPath.contains("/")) {
+			classPath = "/" + classPath;
+		}
+	}
+
+	private static void setAppPath() {
+		File file = new File(getClassPath());
+		appPath = file.getParent() + File.separator;
+	}
+
+	private static void setFilePath() {
+		File file = new File(getAppPath());
+		filePath = file.getParent() + File.separator;
+	}
+}

+ 5 - 18
search-console/src/main/java/com/uas/search/console/core/advice/ExceptionHandlerAdvice.java

@@ -2,18 +2,16 @@
  */
 package com.uas.search.console.core.advice;
 
+import com.uas.search.util.ExceptionUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.ui.ModelMap;
-import com.uas.search.util.StringUtils;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 
-import com.uas.search.exception.SearchException;
-
 /**
  * 基于Application的异常处理,以AOP的形式注册到SpringMVC的处理链
  * 
@@ -27,30 +25,19 @@ public class ExceptionHandlerAdvice {
 
 	/**
 	 * 处理已捕获异常,明确传达给客户端错误码、错误信息
-	 * 
+	 *
 	 * @param e
 	 * @return
 	 */
 	@ExceptionHandler(Throwable.class)
-	public ResponseEntity<ModelMap> handleException(Throwable e) {
+	public ResponseEntity<ModelMap> handleError(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());
-		}
+		map.put("message", e.getMessage());
+		map.put("detailedMessage", ExceptionUtils.getDetailedMessage(e));
 		HttpHeaders headers = new HttpHeaders();
 		headers.add("Content-Type", "application/json; charset=utf-8");
-		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);
 	}
-
 }