This is an automated email from the ASF dual-hosted git repository.

aradzinski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/master by this push:
     new fe32dcb  Fix for NLPCRAFT-166.
fe32dcb is described below

commit fe32dcb934bd3acb942f996d6ac74a8237929c20
Author: Aaron Radzinski <[email protected]>
AuthorDate: Sun Nov 1 00:01:14 2020 -0700

    Fix for NLPCRAFT-166.
---
 .../org/apache/nlpcraft/common/NCException.java    |  2 +-
 .../org/apache/nlpcraft/common/util/NCUtils.scala  | 25 ++++++----------------
 .../model/factories/basic/NCBasicModelFactory.java | 18 ++++++++++++++--
 .../probe/mgrs/deploy/NCDeployManager.scala        | 19 ++++++++++------
 4 files changed, 35 insertions(+), 29 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/NCException.java 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/NCException.java
index 9829616..7f5e681 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/NCException.java
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/NCException.java
@@ -30,7 +30,7 @@ public class NCException extends RuntimeException {
      * @param cause Optional cause of this exception.
      */
     public NCException(String msg, Throwable cause) {
-        super(msg, NCUtils.getOriginCause(cause));
+        super(msg, cause);
     }
 
     /**
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
index 814945e..f06b746 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
@@ -1205,21 +1205,6 @@ object NCUtils extends LazyLogging {
             }
 
     /**
-      *
-      * @param e
-      * @return
-      */
-    @tailrec
-    def getOriginCause(e: Throwable): Throwable =
-        if (e == null)
-            null
-        else
-            e.getCause match {
-                case null ⇒ e // Original cause (bottom of the stack trace).
-                case t ⇒ getOriginCause(t)
-            }
-
-    /**
       * Safely and silently closes the server socket.
       *
       * @param sock Server socket to close.
@@ -1363,7 +1348,9 @@ object NCUtils extends LazyLogging {
             var errMsg = x.getLocalizedMessage
 
             if (errMsg == null)
-                errMsg = "<<null>>"
+                errMsg = "<null>"
+
+            val exClsName = if (!x.isInstanceOf[NCE]) 
s"$ansiRedFg[${x.getClass.getSimpleName}]$ansiReset " else ""
 
             val trace = 
x.getStackTrace.find(!_.getClassName.startsWith("scala.")).getOrElse(x.getStackTrace.head)
 
@@ -1372,12 +1359,12 @@ object NCUtils extends LazyLogging {
 
             val msg =
                 if (fileName == null || lineNum < 0)
-                    errMsg
+                    s"$exClsName$errMsg"
                 else
-                    s"$errMsg $ansiCyanFg->$ansiReset ($fileName:$lineNum)"
+                    s"$exClsName$errMsg $ansiCyanFg->$ansiReset 
($fileName:$lineNum)"
 
             msg.split("\n").foreach(line ⇒ {
-                val s = s"${" " * indent}${if (first) ansiBlue("+- ") else "   
"}${line}"
+                val s = s"${" " * indent}${if (first) ansiBlue("+- ") else "   
"}$line"
 
                 if (err) logger.error(s) else logger.warn(s)
 
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/factories/basic/NCBasicModelFactory.java
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/factories/basic/NCBasicModelFactory.java
index 449df17..e9c9c72 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/factories/basic/NCBasicModelFactory.java
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/factories/basic/NCBasicModelFactory.java
@@ -19,12 +19,14 @@ package org.apache.nlpcraft.model.factories.basic;
 
 import org.apache.nlpcraft.common.*;
 import org.apache.nlpcraft.model.*;
+
+import java.lang.reflect.InvocationTargetException;
 import java.util.*;
 
 /**
  * Default implementation of {@link NCModelFactory} interface.
  * <p>
- * This factory doesn't have any configuration properties and uses {@link 
Class#newInstance()} to construct {@link NCModel}s.
+ * This factory doesn't have any configuration properties and uses {@link 
java.lang.reflect.Constructor#newInstance(Object...)} to construct {@link 
NCModel}s.
  * </p>
  * Basic factory have to be specified in probe configuration. Here's
  * a <code>probe.conf</code> from <a target="github" 
href="https://github.com/apache/incubator-nlpcraft/tree/master/nlpcraft/src/main/scala/org/apache/nlpcraft/examples/names";>Names</a>
 example
@@ -68,8 +70,20 @@ public class NCBasicModelFactory implements NCModelFactory {
         try {
             return type.getConstructor().newInstance();
         }
+        catch (NoSuchMethodException e) {
+            throw new NCException(String.format("Model class does not have 
no-arg constructor: %s", type.getCanonicalName()), e);
+        }
+        catch (InstantiationException e) {
+            throw new NCException(String.format("Model class cannot be an 
abstract class: %s", type.getCanonicalName()), e);
+        }
+        catch (InvocationTargetException e) {
+            throw new NCException(String.format("Model no-arg constructor 
failed: %s", type.getCanonicalName()), e);
+        }
+        catch (ExceptionInInitializerError e) {
+            throw new NCException(String.format("Model class initialization 
failed: %s", type.getCanonicalName()), e);
+        }
         catch (Exception e) {
-            throw new NCException(String.format("Failed to instantiate model: 
%s", type), e);
+            throw new NCException(String.format("Failed to instantiate model: 
%s", type.getCanonicalName()), e);
         }
     }
 
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
index 5b936b0..9b15a7d 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/deploy/NCDeployManager.scala
@@ -30,7 +30,7 @@ import org.apache.nlpcraft.common.ascii.NCAsciiTable
 import org.apache.nlpcraft.common.config.NCConfigurable
 import org.apache.nlpcraft.common.makro.NCMacroParser
 import org.apache.nlpcraft.common.nlp.core.{NCNlpCoreManager, 
NCNlpPorterStemmer}
-import org.apache.nlpcraft.common.util.NCUtils.{DSL_FIX, REGEX_FIX}
+import org.apache.nlpcraft.common.util.NCUtils.{DSL_FIX, REGEX_FIX, escapeJson}
 import org.apache.nlpcraft.model._
 import org.apache.nlpcraft.model.factories.basic.NCBasicModelFactory
 import org.apache.nlpcraft.model.intent.impl.{NCIntentDslCompiler, 
NCIntentSolver}
@@ -546,12 +546,17 @@ object NCDeployManager extends NCService with 
DecorateAsScala {
     @throws[NCE]
     private def makeModelFromSource(cls: Class[_ <: NCModel], src: String): 
NCModel =
         catching(classOf[Throwable]) either mdlFactory.mkModel(cls) match {
-            case Left(e) ⇒
-                throw new NCE(s"Failed to instantiate model [" +
-                    s"cls=${cls.getName}, " +
-                    s"factory=${mdlFactory.getClass.getName}, " +
-                    s"src=$src" +
-                "]", e)
+            case Left(e) ⇒ e match {
+                case _: NCE ⇒ throw e
+                case _ ⇒
+                    throw new NCE(s"Failed to instantiate model [" +
+                        s"cls=${cls.getName}, " +
+                        s"factory=${mdlFactory.getClass.getName}, " +
+                        s"src=$src" +
+                        "]",
+                        e
+                    )
+            }
 
             case Right(model) ⇒ model
         }

Reply via email to