This is my JavaScript code, it handles the promise as you see:
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);
});
}
}
I want to alert in the second function (the "error") the same message I get from the Exception. This is the Java code that throws all of the Exceptions:
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());
}
}
How can I use the "err.data" to find only the Exception message?
Now the alert looks like :