如何处理 Promise 错误? JS/Java

How to handle Promise error? JS/Java

这是我的Java脚本代码,它如您所见处理承诺:

this.buy = function(coupon) {
   var flag = confirm("Are you sure you want to buy coupon #"
     + coupon.id + "?");
   if (flag) {
    var promise = CustomerMockService.buyCoupon(coupon);
    promise.then(function(resp) {
     self.myCoupons = resp.data;
     self.message = "Coupon #" + coupon.id
       + " has been added to your shopping cart.";
    }, function(err) {
     alert(err.data);
    });
   }
  }

我想在第二个函数("error")中提醒我从异常中得到的相同消息。 这是抛出所有异常的 Java 代码:

public void purchaseCoupon(Coupon coupon) throws ExistException, OutOfDateException, FacadeException {
  // ==========================================================================
  // check if the customer did'nt already purchased this coupon.
  // ==========================================================================
  ArrayList<Coupon> list = this.getAllPurchasedCoupons();
  for (int i = 0; i < list.size(); i++) {
   if (list.get(i).getId() == coupon.getId())
    throw new ExistException("can't purchase a coupon you already have");
  }
  // ==========================================================================
  // check if the coupon is not out-of-date.
  // ==========================================================================
  if (coupon.getEnd_date().before(new Date()))
   throw new OutOfDateException("The coupon you trying to purchase is out of date.");
  // ==========================================================================
  // check if there is any coupons left to be purchased.
  // ==========================================================================
  if (coupon.getAmount() > 0)
   coupon.setAmount(coupon.getAmount() - 1);
  else
   throw new ExistException("No coupons left");
  // ==========================================================================
  try {
   couponDAODB.updateCoupon(coupon);
   customer_CouponDAODB.createCustomer_Coupon(this.Cust_id, coupon.getId());
  } catch (DAOException e) {
   throw new FacadeException(e.getMessage());
  }
 }

如何使用 "err.data" 仅查找异常消息? 现在警报看起来像:

据我了解,您收到的是 html 回复,而不是 JSON(后者更可取)。 您需要在服务器端更改对 JSON 的响应。

如果不行,另一种解决方法是更新错误函数:

function(err) {
  // Make html document from response html string
  var div = document.createElement('div');
  div.innerHTML = err.data;
  // Get html element with error message
  var messageBlock = div.querySelector('h1');
  var message = 'Error on server';
  if (messageBlock) {
    // you can parse you message if it's needed
    message = messageBlock.innerText;
  }
  alert(message); // HTTP Status 500 - ...... can't purchase a coupon...
}

请检查您的体系结构,我怀疑异常处理应该在服务方法和方法上完成,并且方法不应该无效。我建议不要抛出异常,而是在该服务方法中捕获它,如果发生 return 错误。问题是您向 java 容器抛出异常,Tomcat 处理它并生成标准错误页面。

我的建议是这样的:

public Response purchaseCoupon(Coupon coupon) {
   Response response = new Response(); //some response structure
   try {
     //service logic goes here
     response.setObject(coupon); 
     response.setStatusCode("200");
     response.setSatatusMessage("OK");
   }catch (DAOException | FacadeException e) {
     response.setStatusCode("E001");
     response.setStatusMessage(e.getMessage());
     //additional logging
   }catch (ExistException ex) {
     response.setStatusCode("E010");
     response.setStatusMessage(ex.getMessage());
     //additional logging
   }
  return response;
}