Browse Source

AOP处理异常时只针对Throwable进行处理,不再细分

sunyj 8 years ago
parent
commit
7bec990327

+ 24 - 78
src/main/java/com/uas/report/core/advice/ExceptionHandlerAdvice.java

@@ -3,7 +3,6 @@
 package com.uas.report.core.advice;
 
 import java.io.IOException;
-import java.sql.SQLRecoverableException;
 
 import javax.servlet.http.HttpServletResponse;
 
@@ -21,98 +20,42 @@ import com.uas.report.core.exception.ReportException;
 import com.uas.report.util.StringUtils;
 
 /**
- * <p>
  * 基于Application的异常处理,以AOP的形式注册到SpringMVC的处理链
- * </p>
- * <p>
- * 正常的业务流程,只需抛出对应的异常和相关的信息
- * </P>
- * <p>
- * 不同的错误,对应不同的方法来处理
- * </p>
- * <p>
- * 可以用不同的HttpStatus来表示具体的异常类型
- * </p>
- * <p>
- * 客户端可以基于对应的HttpStatus Code做出最有利于自己的处理
- * </p>
- * 
- * @author yingp
  * 
+ * @author sunyj
+ * @since 2017年7月13日 上午10:28:37
  */
 @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");
-		ex.printStackTrace();
-		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");
-		return new ResponseEntity<String>("\u53C2\u6570\u9519\u8BEF", headers, HttpStatus.INTERNAL_SERVER_ERROR);
-	}
+	private static Logger logger = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);
 
 	/**
 	 * 处理已捕获异常,明确传达给客户端错误码、错误信息
 	 * 
-	 * @see ReportException
-	 * @param ex
+	 * @param e
 	 * @return
 	 */
-	@ExceptionHandler(ReportException.class)
-	public ResponseEntity<ModelMap> handleSystemError(ReportException ex) {
-		HttpHeaders headers = new HttpHeaders();
-		headers.add("Content-Type", "application/json; charset=utf-8");
+	@ExceptionHandler(Throwable.class)
+	public ResponseEntity<ModelMap> handleError(Throwable e) {
+		logger.error("", e);
 		ModelMap map = new ModelMap();
 		map.put("success", false);
-		// 将换行符、Tab空格替换为前端的表示
-		map.put("message", ex.getMessage().replaceAll("\n", "<br/>").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;"));
-		if (!StringUtils.isEmpty(ex.getDetailedMessage())) {
-			logger.error(ex.getDetailedMessage());
-			map.put("detailedMessage",
-					ex.getDetailedMessage().replaceAll("\n", "<br/>").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;"));
+		if (e instanceof ReportException) {
+			map.put("message", e.getMessage());
 		} else {
-			logger.error(ex.getMessage());
+			map.put("message", e.getClass().getSimpleName() + ": " + e.getMessage());
+		}
+		HttpHeaders headers = new HttpHeaders();
+		headers.add("Content-Type", "application/json; charset=utf-8");
+		if (e instanceof ReportException) {
+			ReportException exception = (ReportException) 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.BAD_REQUEST);
+		return new ResponseEntity<ModelMap>(map, headers, HttpStatus.INTERNAL_SERVER_ERROR);
 	}
 
 	/**
@@ -121,9 +64,12 @@ public class ExceptionHandlerAdvice {
 	 * @param response
 	 * @param status
 	 * @param message
+	 * @param ip
 	 * @throws IOException
 	 */
-	public static void handleError(HttpServletResponse response, Integer status, String message) throws IOException {
+	public static void handleError(HttpServletResponse response, Integer status, String message, String ip)
+			throws IOException {
+		logger.error(message + ":" + ip);
 		response.setHeader("Content-type", "text/html;charset=UTF-8");
 		response.setStatus(status);
 		JSONObject jsonObject = new JSONObject();

+ 3 - 2
src/main/java/com/uas/report/filter/FileDeleteFilter.java

@@ -33,8 +33,9 @@ public class FileDeleteFilter implements Filter {
 		HttpServletRequest httpRequest = (HttpServletRequest) request;
 		HttpServletResponse httpResponse = (HttpServletResponse) response;
 		// 局域网内才允许执行
-		if (!IpHelper.isLAN(IpHelper.getIp(httpRequest))) {
-			ExceptionHandlerAdvice.handleError(httpResponse, HttpServletResponse.SC_FORBIDDEN, "没有权限");
+		String ip = IpHelper.getIp(httpRequest);
+		if (!IpHelper.isLAN(ip)) {
+			ExceptionHandlerAdvice.handleError(httpResponse, HttpServletResponse.SC_FORBIDDEN, "没有权限", ip);
 		} else {
 			chain.doFilter(httpRequest, response);
 		}

+ 2 - 2
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -217,7 +217,7 @@ public class PrintServiceImpl implements PrintService {
 			}
 			result.put("data", data);
 			return result;
-		} catch (Exception e) {
+		} catch (Throwable e) {
 			throw new ReportException(e).setDetailedMessage(e);
 		} finally {
 			if (connection != null) {
@@ -669,7 +669,7 @@ public class PrintServiceImpl implements PrintService {
 			int count = getCount(jrxmlFilePath, whereCondition, otherParameters, connection);
 			logger.info("count... " + count);
 			return count >= MAX_RECODR_SIZE;
-		} catch (Exception e) {
+		} catch (Throwable e) {
 			throw new ReportException(e).setDetailedMessage(e);
 		} finally {
 			if (connection != null) {