这是我的JavaScript代码,它可以处理您的承诺:
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);
});
}
}

我想在第二个函数("错误")中提醒我从异常中获得的相同消息。 这是抛出所有异常的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());
}
}
&#13;
答案 0 :(得分:0)
据我了解,您收到的是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...
}
答案 1 :(得分:0)
请检查您的架构,我怀疑应该对服务方法进行异常处理,方法和方法不应该是无效的。我建议不要抛出异常,但在该服务方法中捕获它并返回错误(如果发生)。问题是你向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;
}