From 4739f126a2172d43034bbe57e450817d46f6667d Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Sat, 26 Nov 2022 00:19:22 -0700 Subject: [PATCH] [jrubyscripting] log Ruby stacktrace on exception from JRuby (#13778) Signed-off-by: Cody Cutrer --- .../JRubyScriptEngineConfiguration.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineConfiguration.java b/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineConfiguration.java index dbf631fdd..96488ac59 100644 --- a/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineConfiguration.java +++ b/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineConfiguration.java @@ -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 */