Browse Source

优化异常类

周浩 9 years ago
parent
commit
9a0e1adcbd

+ 7 - 3
hsweb-web-core/src/main/java/org/hsweb/web/exception/AuthorizeException.java

@@ -7,10 +7,14 @@ public class AuthorizeException extends BusinessException {
     private static final long serialVersionUID = 2422918455013900645L;
 
     public AuthorizeException(String message) {
-        super(message);
+        this(message, 401);
     }
 
-    public AuthorizeException(String message, Throwable cause) {
-        super(message, cause);
+    public AuthorizeException(String message, int status) {
+        super(message, status);
+    }
+
+    public AuthorizeException(String message, Throwable cause, int status) {
+        super(message, cause, status);
     }
 }

+ 12 - 1
hsweb-web-core/src/main/java/org/hsweb/web/exception/BusinessException.java

@@ -6,12 +6,23 @@ package org.hsweb.web.exception;
  */
 public class BusinessException extends RuntimeException {
     private static final long serialVersionUID = -695542791928644578L;
+    private int status = 400;
 
     public BusinessException(String message) {
+        this(message, 400);
+    }
+
+    public BusinessException(String message, int status) {
         super(message);
+        this.status = status;
     }
 
-    public BusinessException(String message, Throwable cause) {
+    public BusinessException(String message, Throwable cause, int status) {
         super(message, cause);
+        this.status = status;
+    }
+
+    public int getStatus() {
+        return status;
     }
 }

+ 24 - 1
hsweb-web-core/src/main/java/org/hsweb/web/exception/ExceptionHolderConfig.java

@@ -1,21 +1,44 @@
 package org.hsweb.web.exception;
 
 import org.hsweb.web.message.ResponseMessage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.servlet.ModelAndView;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 import java.util.Map;
 
 /**
  * Created by zhouhao on 16-4-26.
  */
 @Configuration
-public class ExceptionHolderConfig {
+public class ExceptionHandlerConfiguration {
+    private Logger logger = LoggerFactory.getLogger(this.getClass());
+
     @Bean
     public DefaultErrorAttributes errorAttributes() {
         return new DefaultErrorAttributes() {
+            @Override
+            public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
+                super.resolveException(request, response, handler, ex);
+                if (ex instanceof BusinessException) {
+                    try {
+                        response.sendError(((BusinessException) ex).getStatus());
+                    } catch (IOException e) {
+                        logger.error("response.sendError", e);
+                    }
+                }
+                return null;
+            }
+
             @Override
             public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes,
                                                           boolean includeStackTrace) {