]> git.basschouten.com Git - openhab-addons.git/commitdiff
[jrubyscripting] log Ruby stacktrace on exception from JRuby (#13778)
authorCody Cutrer <cody@cutrer.us>
Sat, 26 Nov 2022 07:19:22 +0000 (00:19 -0700)
committerGitHub <noreply@github.com>
Sat, 26 Nov 2022 07:19:22 +0000 (08:19 +0100)
Signed-off-by: Cody Cutrer <cody@cutrer.us>
bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineConfiguration.java

index dbf631fdd54b8bf6628bac6e42498465163584a8..96488ac598a1d2aafa61478b914982ffe13266a2 100644 (file)
@@ -199,7 +199,7 @@ public class JRubyScriptEngineConfiguration {
             logger.trace("Gem install code:\n{}", gemCommand);
             engine.eval(gemCommand);
         } catch (ScriptException e) {
-            logger.warn("Error installing Gems: {}", e.getMessage());
+            logger.warn("Error installing Gems", unwrap(e));
         }
     }
 
@@ -221,7 +221,7 @@ public class JRubyScriptEngineConfiguration {
                         logger.trace("Injecting require statement: {}", requireStatement);
                         engine.eval(requireStatement);
                     } catch (ScriptException e) {
-                        logger.warn("Error evaluating statement {}: {}", requireStatement, e.getMessage());
+                        logger.warn("Error evaluating `{}`", requireStatement, unwrap(e));
                     }
                 });
     }
@@ -239,7 +239,7 @@ public class JRubyScriptEngineConfiguration {
                 logger.trace("Setting Ruby environment with code: {} ", environmentSetting);
                 engine.eval(environmentSetting);
             } catch (ScriptException e) {
-                logger.warn("Error setting ruby environment", e);
+                logger.warn("Error setting Ruby environment", unwrap(e));
             }
         });
 
@@ -267,7 +267,7 @@ public class JRubyScriptEngineConfiguration {
             try {
                 engine.eval(code);
             } catch (ScriptException exception) {
-                logger.warn("Error setting $LOAD_PATH from RUBYLIB='{}': {}", rubyLib.get(), exception.getMessage());
+                logger.warn("Error setting $LOAD_PATH from RUBYLIB='{}'", rubyLib.get(), unwrap(exception));
             }
         }
     }
@@ -293,6 +293,20 @@ public class JRubyScriptEngineConfiguration {
                 .filter(element -> element.getValue().isPresent());
     }
 
+    /**
+     * Unwraps the cause of an exception, if it has one.
+     *
+     * Since a user cares about the _Ruby_ stack trace of the throwable, not
+     * the details of where openHAB called it.
+     */
+    private Throwable unwrap(Throwable e) {
+        Throwable cause = e.getCause();
+        if (cause != null) {
+            return cause;
+        }
+        return e;
+    }
+
     /**
      * Inner static companion class for configuration elements
      */