diff --git a/src/main/java/ltd/newbee/mall/common/NewBeeMallException.java b/src/main/java/ltd/newbee/mall/common/NewBeeMallException.java new file mode 100644 index 00000000..494ceb7b --- /dev/null +++ b/src/main/java/ltd/newbee/mall/common/NewBeeMallException.java @@ -0,0 +1,21 @@ +package ltd.newbee.mall.common; + +public class NewBeeMallException extends RuntimeException { + + public NewBeeMallException() { + } + + public NewBeeMallException(String message) { + super(message); + } + + /** + * 丢出一个异常 + * + * @param message + */ + public static void fail(String message) { + throw new NewBeeMallException(message); + } + +} diff --git a/src/main/java/ltd/newbee/mall/controller/common/NewBeeMallExceptionHandler.java b/src/main/java/ltd/newbee/mall/controller/common/NewBeeMallExceptionHandler.java new file mode 100644 index 00000000..628307d8 --- /dev/null +++ b/src/main/java/ltd/newbee/mall/controller/common/NewBeeMallExceptionHandler.java @@ -0,0 +1,46 @@ +package ltd.newbee.mall.controller.common; + +import ltd.newbee.mall.common.NewBeeMallException; +import ltd.newbee.mall.util.Result; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; + +/** + * newbee-mall全局异常处理 + */ +@RestControllerAdvice +public class NewBeeMallExceptionHandler { + + @ExceptionHandler(Exception.class) + public Object handleException(Exception e, HttpServletRequest req) { + Result result = new Result(); + result.setResultCode(500); + //区分是否为自定义异常 + if (e instanceof NewBeeMallException) { + result.setMessage(e.getMessage()); + } else { + result.setMessage("未知异常,请联系管理员"); + } + //检查请求是否为ajax, 如果是 ajax 请求则返回 Result json串, 如果不是 ajax 请求则返回 error 视图 + String contentTypeHeader = req.getHeader("Content-Type"); + String acceptHeader = req.getHeader("Accept"); + String xRequestedWith = req.getHeader("X-Requested-With"); + if ((contentTypeHeader != null && contentTypeHeader.contains("application/json")) + || (acceptHeader != null && acceptHeader.contains("application/json")) + || "XMLHttpRequest".equalsIgnoreCase(xRequestedWith)) { + return result; + } else { + ModelAndView modelAndView = new ModelAndView(); + modelAndView.addObject("message", e.getMessage()); + modelAndView.addObject("url", req.getRequestURL()); + modelAndView.addObject("stackTrace", e.getStackTrace()); + modelAndView.addObject("author", "十三"); + modelAndView.addObject("ltd", "新峰商城"); + modelAndView.setViewName("error/error"); + return modelAndView; + } + } +} diff --git a/src/main/java/ltd/newbee/mall/controller/mall/OrderController.java b/src/main/java/ltd/newbee/mall/controller/mall/OrderController.java index ca194cb7..936a9322 100644 --- a/src/main/java/ltd/newbee/mall/controller/mall/OrderController.java +++ b/src/main/java/ltd/newbee/mall/controller/mall/OrderController.java @@ -1,6 +1,7 @@ package ltd.newbee.mall.controller.mall; import ltd.newbee.mall.common.Constants; +import ltd.newbee.mall.common.NewBeeMallException; import ltd.newbee.mall.common.ServiceResultEnum; import ltd.newbee.mall.controller.vo.NewBeeMallOrderDetailVO; import ltd.newbee.mall.controller.vo.NewBeeMallShoppingCartItemVO; @@ -62,25 +63,16 @@ public String saveOrder(HttpSession httpSession) { List myShoppingCartItems = newBeeMallShoppingCartService.getMyShoppingCartItems(user.getUserId()); if (StringUtils.isEmpty(user.getAddress().trim())) { //无收货地址 - return "error/error_5xx"; + NewBeeMallException.fail(ServiceResultEnum.NULL_ADDRESS_ERROR.getResult()); } if (CollectionUtils.isEmpty(myShoppingCartItems)) { //购物车中无数据则跳转至错误页 - return "error/error_5xx"; - } else { - //保存订单并返回订单号 - String saveOrderResult = newBeeMallOrderService.saveOrder(user, myShoppingCartItems); - if (ServiceResultEnum.ORDER_PRICE_ERROR.getResult().equals(saveOrderResult) - || ServiceResultEnum.ORDER_PRICE_ERROR.getResult().equals(saveOrderResult) - || ServiceResultEnum.DB_ERROR.getResult().equals(saveOrderResult) - || ServiceResultEnum.SHOPPING_ITEM_COUNT_ERROR.getResult().equals(saveOrderResult) - || ServiceResultEnum.SHOPPING_ITEM_ERROR.getResult().equals(saveOrderResult)) { - //订单生成失败 - return "error/error_5xx"; - } - //跳转到订单详情页 - return "redirect:/orders/" + saveOrderResult; + NewBeeMallException.fail(ServiceResultEnum.SHOPPING_ITEM_ERROR.getResult()); } + //保存订单并返回订单号 + String saveOrderResult = newBeeMallOrderService.saveOrder(user, myShoppingCartItems); + //跳转到订单详情页 + return "redirect:/orders/" + saveOrderResult; } @PutMapping("/orders/{orderNo}/cancel") diff --git a/src/main/java/ltd/newbee/mall/service/impl/NewBeeMallOrderServiceImpl.java b/src/main/java/ltd/newbee/mall/service/impl/NewBeeMallOrderServiceImpl.java index b7fdf7b2..c3e333c6 100644 --- a/src/main/java/ltd/newbee/mall/service/impl/NewBeeMallOrderServiceImpl.java +++ b/src/main/java/ltd/newbee/mall/service/impl/NewBeeMallOrderServiceImpl.java @@ -1,9 +1,6 @@ package ltd.newbee.mall.service.impl; -import ltd.newbee.mall.common.NewBeeMallOrderStatusEnum; -import ltd.newbee.mall.common.PayStatusEnum; -import ltd.newbee.mall.common.PayTypeEnum; -import ltd.newbee.mall.common.ServiceResultEnum; +import ltd.newbee.mall.common.*; import ltd.newbee.mall.controller.vo.*; import ltd.newbee.mall.dao.NewBeeMallGoodsMapper; import ltd.newbee.mall.dao.NewBeeMallOrderItemMapper; @@ -188,11 +185,11 @@ public String saveOrder(NewBeeMallUserVO user, List newBeeMallGoodsMap.get(shoppingCartItemVO.getGoodsId()).getStockNum()) { - return ServiceResultEnum.SHOPPING_ITEM_COUNT_ERROR.getResult(); + NewBeeMallException.fail(ServiceResultEnum.SHOPPING_ITEM_COUNT_ERROR.getResult()); } } //删除购物项 @@ -201,7 +198,7 @@ public String saveOrder(NewBeeMallUserVO user, List stockNumDTOS = BeanUtil.copyList(myShoppingCartItems, StockNumDTO.class); int updateStockNumResult = newBeeMallGoodsMapper.updateStockNum(stockNumDTOS); if (updateStockNumResult < 1) { - return ServiceResultEnum.SHOPPING_ITEM_COUNT_ERROR.getResult(); + NewBeeMallException.fail(ServiceResultEnum.SHOPPING_ITEM_COUNT_ERROR.getResult()); } //生成订单号 String orderNo = NumberUtil.genOrderNo(); @@ -216,7 +213,7 @@ public String saveOrder(NewBeeMallUserVO user, List getOrderItems(Long id) { } return null; } -} +} \ No newline at end of file diff --git a/src/main/resources/templates/error/error.html b/src/main/resources/templates/error/error.html new file mode 100644 index 00000000..4a1b2d12 --- /dev/null +++ b/src/main/resources/templates/error/error.html @@ -0,0 +1,25 @@ + + + + + NEW BEE ERROR PAGE + + + +
+

你的请求出错了

+

+

+

报告问题

+ 提交issue     + QQ交流群 +

异常堆栈跟踪日志

+
+ +
+

Copy Right

+

+

+
+ +