summaryrefslogtreecommitdiff
path: root/src/main/java/com/mesasoft/cn/exception
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/mesasoft/cn/exception')
-rw-r--r--src/main/java/com/mesasoft/cn/exception/BusinessException.java42
-rw-r--r--src/main/java/com/mesasoft/cn/exception/GlobalExceptionHandler.java36
2 files changed, 78 insertions, 0 deletions
diff --git a/src/main/java/com/mesasoft/cn/exception/BusinessException.java b/src/main/java/com/mesasoft/cn/exception/BusinessException.java
new file mode 100644
index 0000000..90f70b1
--- /dev/null
+++ b/src/main/java/com/mesasoft/cn/exception/BusinessException.java
@@ -0,0 +1,42 @@
+package com.mesasoft.cn.exception;
+
+import lombok.*;
+
+@EqualsAndHashCode(callSuper = true)
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class BusinessException extends RuntimeException {
+
+ /**
+ * 异常代码
+ */
+ @Builder.Default
+ private int status = 500;
+
+ @Builder.Default
+ private String code = "500";
+
+ /**
+ * 异常信息
+ */
+ private String message;
+
+ public BusinessException(String message) {
+ this.message = message;
+ }
+
+
+ public BusinessException(String message, Throwable e) {
+ super(message, e);
+ }
+
+ public BusinessException(int status, String code, String message, Throwable e) {
+ super(message, e);
+ this.status = status;
+ this.code = code;
+ }
+
+
+}
diff --git a/src/main/java/com/mesasoft/cn/exception/GlobalExceptionHandler.java b/src/main/java/com/mesasoft/cn/exception/GlobalExceptionHandler.java
new file mode 100644
index 0000000..7c21c36
--- /dev/null
+++ b/src/main/java/com/mesasoft/cn/exception/GlobalExceptionHandler.java
@@ -0,0 +1,36 @@
+package com.mesasoft.cn.exception;
+
+import com.alibaba.fastjson.support.spring.FastJsonJsonView;
+import com.zhazhapan.modules.constant.ValueConsts;
+import com.zhazhapan.util.Checker;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.servlet.HandlerExceptionResolver;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author pantao
+ * @since 2018/2/5
+ */
+public class GlobalExceptionHandler implements HandlerExceptionResolver {
+
+ @Override
+ public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
+ Exception ex) {
+ ModelAndView mv = new ModelAndView();
+ FastJsonJsonView view = new FastJsonJsonView();
+ Map<String, Object> attributes = new HashMap<>(ValueConsts.TWO_INT);
+ attributes.put("code", "502");
+ attributes.put("message", ex.getMessage());
+ String queryString = request.getQueryString();
+ attributes.put("url", request.getRequestURI() + (Checker.isEmpty(queryString) ? "" : "?" + queryString));
+ view.setAttributesMap(attributes);
+ mv.setView(view);
+ mv.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
+ return mv;
+ }
+}