Fixes issue 31451: OBException logged when it should not
authorAlvaro Ferraz <alvaro.ferraz@openbravo.com>
Thu, 12 Nov 2015 13:02:15 +0100
changeset 27865 425220984e4e
parent 27864 31b64687c6c0
child 27866 e991e8220a8a
Fixes issue 31451: OBException logged when it should not

DefaultJsonDataService will check if it is needed to log localThrowable exception, in case it is an OBException.
New method added in OBException to allow throw exceptions without throwable cause and without log them.
modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java
src/org/openbravo/base/exception/OBException.java
--- a/modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java	Fri Oct 30 00:43:32 2015 +0100
+++ b/modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java	Thu Nov 12 13:02:15 2015 +0100
@@ -830,7 +830,11 @@
             .getCause();
         localThrowable = batchException.getNextException();
       }
-      log.error(localThrowable.getMessage(), localThrowable);
+      if (!(localThrowable instanceof OBException)
+          || (localThrowable instanceof OBException && ((OBException) localThrowable)
+              .isLogExceptionNeeded())) {
+        log.error(localThrowable.getMessage(), localThrowable);
+      }
       return JsonUtils.convertExceptionToJson(localThrowable);
     }
 
--- a/src/org/openbravo/base/exception/OBException.java	Fri Oct 30 00:43:32 2015 +0100
+++ b/src/org/openbravo/base/exception/OBException.java	Thu Nov 12 13:02:15 2015 +0100
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2008-2014 Openbravo SLU 
+ * All portions are Copyright (C) 2008-2015 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -30,6 +30,7 @@
 public class OBException extends RuntimeException {
 
   private static final long serialVersionUID = 1L;
+  private boolean logExceptionNeeded;
 
   public OBException() {
     super();
@@ -40,8 +41,17 @@
     this(message, cause, true);
   }
 
+  public OBException(String message, boolean logException) {
+    super(message);
+    logExceptionNeeded = logException;
+    if (logException) {
+      getLogger().error(message, this);
+    }
+  }
+
   public OBException(String message, Throwable cause, boolean logException) {
     super(message, cause);
+    logExceptionNeeded = logException;
     if (logException) {
       getLogger().error(message, cause);
     }
@@ -66,4 +76,13 @@
   protected Logger getLogger() {
     return Logger.getLogger(this.getClass());
   }
+
+  /**
+   * This method returns if log exception is needed.
+   * 
+   * @return the logExceptionNeeded
+   */
+  public boolean isLogExceptionNeeded() {
+    return logExceptionNeeded;
+  }
 }