0

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 : that

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Daniel Liverant
  • 57
  • 1
  • 2
  • 9

2 Answers2

0

As I understand, you receive html response, not JSON(which is more preferable). You need to change response to the JSON at the server side.

If it's impossible, another solution can be update error function:

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...
}
O. Borcuhin
  • 234
  • 2
  • 5
  • that is similar to what I wanted, with a little bit parsing of the message (message.substring(lastIndexOf(':')) and this is the alert I wanted. thank you! – Daniel Liverant Jun 15 '17 at 16:24
0

Please review you architecture, I suspect that exception handling should be done on the service method and method and method shouldn't be void. I suggest do no throw exception, but catch it in that service method and return error if it occurs. Problem is that you throw exception to java container and Tomcat handles it and produces standard error page.

My suggestion do like this:

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;
}
Saulius Next
  • 1,340
  • 10
  • 16