Skip to content

Instantly share code, notes, and snippets.

@headius
Created September 25, 2015 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save headius/faf3728943e47fe81ddb to your computer and use it in GitHub Desktop.
Save headius/faf3728943e47fe81ddb to your computer and use it in GitHub Desktop.
Ugly hack to disable stack traces for type-conversion exceptions
diff --git a/core/src/main/java/org/jruby/Ruby.java b/core/src/main/java/org/jruby/Ruby.java
index 3be97a4..50a2a0c 100644
--- a/core/src/main/java/org/jruby/Ruby.java
+++ b/core/src/main/java/org/jruby/Ruby.java
@@ -4067,6 +4067,19 @@ public final class Ruby implements Constantizable {
}
/**
+ * @param exceptionClass
+ * @param message
+ * @return
+ */
+ public RaiseException newLightweightRaiseException(RubyClass exceptionClass, String message) {
+ if (RubyInstanceConfig.ERRNO_BACKTRACE) {
+ return new RaiseException(this, exceptionClass, message, true);
+ } else {
+ return new RaiseException(this, exceptionClass, ERRNO_BACKTRACE_MESSAGE, RubyArray.newEmptyArray(this), true);
+ }
+ }
+
+ /**
* Generate one of the ERRNO exceptions. This differs from the normal logic
* by avoiding the generation of a backtrace. Many ERRNO values are expected,
* such as EAGAIN, and JRuby pays a very high cost to generate backtraces that
diff --git a/core/src/main/java/org/jruby/RubyKernel.java b/core/src/main/java/org/jruby/RubyKernel.java
index 715dfc0..2ebaa0b 100644
--- a/core/src/main/java/org/jruby/RubyKernel.java
+++ b/core/src/main/java/org/jruby/RubyKernel.java
@@ -389,7 +389,7 @@ public class RubyKernel {
return RubyFloat.newFloat(runtime, RubyBignum.big2dbl((RubyBignum)object));
} else if(object instanceof RubyString){
if(((RubyString) object).getByteList().getRealSize() == 0){ // rb_cstr_to_dbl case
- throw runtime.newArgumentError("invalid value for Float(): " + object.inspect());
+ throw runtime.newLightweightRaiseException(runtime.getArgumentError(), "invalid value for Float(): " + object.inspect());
}
RubyString arg = (RubyString)object;
if (arg.toString().startsWith("0x")) {
@@ -397,7 +397,7 @@ public class RubyKernel {
}
return RubyNumeric.str2fnum(runtime, arg, true);
} else if(object.isNil()){
- throw runtime.newTypeError("can't convert nil into Float");
+ throw runtime.newLightweightRaiseException(runtime.getTypeError(), "can't convert nil into Float");
} else {
return (RubyFloat)TypeConverter.convertToType19(object, runtime.getFloat(), "to_f");
}
@@ -413,7 +413,7 @@ public class RubyKernel {
if (arg instanceof RubyArray && ((RubyArray) arg).isEmpty()) {
return RubyHash.newHash(runtime);
}
- throw runtime.newTypeError("can't convert " + arg.getMetaClass() + " into Hash");
+ throw runtime.newLightweightRaiseException(runtime.getTypeError(), "can't convert " + arg.getMetaClass() + " into Hash");
}
return tmp;
}
diff --git a/core/src/main/java/org/jruby/RubyNumeric.java b/core/src/main/java/org/jruby/RubyNumeric.java
index 1ead4fe..fa59d76 100644
--- a/core/src/main/java/org/jruby/RubyNumeric.java
+++ b/core/src/main/java/org/jruby/RubyNumeric.java
@@ -380,7 +380,7 @@ public class RubyNumeric extends RubyObject {
return new RubyFloat(runtime, caller.yield(arg, strict));
} catch (NumberFormatException e) {
if (strict) {
- throw runtime.newArgumentError("invalid value for Float(): "
+ throw runtime.newLightweightRaiseException(runtime.getArgumentError(), "invalid value for Float(): "
+ arg.callMethod(runtime.getCurrentContext(), "inspect").toString());
}
return new RubyFloat(runtime,ZERO);
diff --git a/core/src/main/java/org/jruby/util/ConvertBytes.java b/core/src/main/java/org/jruby/util/ConvertBytes.java
index bb3838d..51fe96d 100644
--- a/core/src/main/java/org/jruby/util/ConvertBytes.java
+++ b/core/src/main/java/org/jruby/util/ConvertBytes.java
@@ -792,6 +792,6 @@ public class ConvertBytes {
*/
private void invalidString(String type) {
IRubyObject s = RubyString.newString(runtime, _str).inspect();
- throw runtime.newArgumentError("invalid value for " + type + ": " + s);
+ throw runtime.newLightweightRaiseException(runtime.getArgumentError(), "invalid value for " + type + ": " + s);
}
}
diff --git a/core/src/main/java/org/jruby/util/TypeConverter.java b/core/src/main/java/org/jruby/util/TypeConverter.java
index 2f25b88..a5eb57e 100644
--- a/core/src/main/java/org/jruby/util/TypeConverter.java
+++ b/core/src/main/java/org/jruby/util/TypeConverter.java
@@ -118,7 +118,8 @@ public class TypeConverter {
IRubyObject val = convertToType19(obj, target, convertMethod, true);
if (!target.isInstance(val)) {
String cname = obj.getMetaClass().toString();
- throw obj.getRuntime().newTypeError("can't convert " + cname + " to " + target.getName() + " (" + cname + "#" + convertMethod + " gives " + val.getMetaClass() + ")");
+ Ruby runtime = obj.getRuntime();
+ throw runtime.newLightweightRaiseException(runtime.getTypeError(), "can't convert " + cname + " to " + target.getName() + " (" + cname + "#" + convertMethod + " gives " + val.getMetaClass() + ")");
}
return val;
}
@@ -365,7 +366,8 @@ public class TypeConverter {
}
private static void raiseIntegerBaseError(ThreadContext context) {
- throw context.runtime.newArgumentError("base specified for non string value");
+ Ruby runtime = context.runtime;
+ throw runtime.newLightweightRaiseException(runtime.getArgumentError(), "base specified for non string value");
}
@Deprecated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment