瀏覽代碼

HttpRequestMethodNotSupportedException 과 그 외 Exception에 대한 Handler 추가

gagamel 4 年之前
父節點
當前提交
6f60ab54f8
共有 1 個文件被更改,包括 57 次插入49 次删除
  1. 57 49
      src/main/java/com/style24/core/support/controller/TscBaseController.java

+ 57 - 49
src/main/java/com/style24/core/support/controller/TscBaseController.java

@@ -1,5 +1,8 @@
 package com.style24.core.support.controller;
 
+import java.lang.reflect.UndeclaredThrowableException;
+import java.sql.SQLException;
+import java.sql.SQLRecoverableException;
 import java.util.Set;
 
 import javax.validation.ConstraintViolation;
@@ -7,15 +10,20 @@ import javax.validation.Validation;
 import javax.validation.Validator;
 
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.apache.ibatis.binding.BindingException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.propertyeditors.StringTrimmerEditor;
 import org.springframework.core.env.Environment;
+import org.springframework.web.HttpRequestMethodNotSupportedException;
 import org.springframework.web.bind.WebDataBinder;
 import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.InitBinder;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.ResponseBody;
 
+import com.fasterxml.jackson.databind.JsonMappingException;
 import com.style24.core.biz.thirdparty.KollusApi;
 import com.style24.core.support.message.TscMessageByLocale;
 
@@ -98,55 +106,55 @@ public class TscBaseController {
 //		errorLogging(ex);
 //		return GagaResponse.error(GagaResponseStatus.BAD_REQUEST.getCode(), ex.getMessage());
 //	}
-//
-//	/**
-//	 * HttpRequestMethodNotSupportedException Handler
-//	 * 
-//	 * @param e - HttpRequestMethodNotSupportedException, SQLRecoverableException
-//	 * @return forwarding URI
-//	 */
-//	@ExceptionHandler({HttpRequestMethodNotSupportedException.class, SQLRecoverableException.class})
-//	public String handleException(HttpRequestMethodNotSupportedException ex) {
-//		return "forward:/error/500";
-//	}
-//
-//	@ExceptionHandler(Throwable.class)
-//	@ResponseBody
-//	public GagaResponse handleException(Throwable throwable) {
-//		errorLogging(throwable);
-//
-//		Throwable rootCause = ExceptionUtils.getRootCause(throwable);
-//
-//		if (rootCause != null) {
-//			throwable = rootCause;
-//		}
-//
-//		if (throwable instanceof SQLException || throwable instanceof BindingException
-//			|| throwable instanceof JsonMappingException
-//			|| throwable instanceof UndeclaredThrowableException) {
-//			String message = String.format("데이터 처리중 에러가 발생하였습니다. 시스템 관리자에게 문의하세요.");
-//			return GagaResponse.error(GagaResponseStatus.INTERNAL_SERVER_ERROR.getCode(), message);
-//		}
-//
-//		return GagaResponse.error(GagaResponseStatus.INTERNAL_SERVER_ERROR.getCode(), throwable.getMessage());
-//	}
-//
-//	private void errorLogging(Throwable throwable) {
-//		if (log.isErrorEnabled()) {
-//			Throwable rootCause = ExceptionUtils.getRootCause(throwable);
-//
-//			if (rootCause != null) {
-//				throwable = rootCause;
-//			}
-//
-//			if (throwable.getMessage() != null) {
-//				log.error(throwable.getMessage(), throwable);
-//			} else {
-//				log.error("ERROR", throwable);
-//			}
-//		}
-//	}
-//
+
+	/**
+	 * HttpRequestMethodNotSupportedException Handler
+	 * 
+	 * @param e - HttpRequestMethodNotSupportedException, SQLRecoverableException
+	 * @return forwarding URI
+	 */
+	@ExceptionHandler({HttpRequestMethodNotSupportedException.class, SQLRecoverableException.class})
+	public String handleException(HttpRequestMethodNotSupportedException ex) {
+		return "forward:/error";
+	}
+
+	@ExceptionHandler(Throwable.class)
+	@ResponseBody
+	public GagaResponse handleException(Throwable throwable) {
+		errorLogging(throwable);
+
+		Throwable rootCause = ExceptionUtils.getRootCause(throwable);
+
+		if (rootCause != null) {
+			throwable = rootCause;
+		}
+
+		if (throwable instanceof SQLException || throwable instanceof BindingException
+			|| throwable instanceof JsonMappingException
+			|| throwable instanceof UndeclaredThrowableException) {
+			String message = String.format("데이터 처리중 에러가 발생하였습니다. 시스템 관리자에게 문의하세요.");
+			return GagaResponse.error(GagaResponseStatus.INTERNAL_SERVER_ERROR.getCode(), message);
+		}
+
+		return GagaResponse.error(GagaResponseStatus.INTERNAL_SERVER_ERROR.getCode(), throwable.getMessage());
+	}
+
+	private void errorLogging(Throwable throwable) {
+		if (log.isErrorEnabled()) {
+			Throwable rootCause = ExceptionUtils.getRootCause(throwable);
+
+			if (rootCause != null) {
+				throwable = rootCause;
+			}
+
+			if (throwable.getMessage() != null) {
+				log.error(throwable.getMessage(), throwable);
+			} else {
+				log.error("ERROR", throwable);
+			}
+		}
+	}
+
 //	@ExceptionHandler(MethodArgumentNotValidException.class)
 //	@ResponseBody
 //	public Object processValidationError(MethodArgumentNotValidException ex) {