bubble up java stacktrace from wrapped exception

move exception builder in RubyUtil

cosmetic

cosmetic
This commit is contained in:
Colin Surprenant 2017-09-13 16:31:49 -04:00
parent 079cfe5bc1
commit 60a47290b5
4 changed files with 59 additions and 10 deletions

View file

@ -0,0 +1,47 @@
package org.logstash;
import org.jruby.NativeException;
import org.jruby.Ruby;
import org.jruby.exceptions.RaiseException;
/**
* Utilities around interaction with the {@link Ruby} runtime.
*/
public final class RubyUtil {
/**
* Name of the Logstash JRuby module we register.
*/
public static final String LS_MODULE_NAME = "LogStash";
/**
* Reference to the global {@link Ruby} runtime.
*/
public static final Ruby RUBY = setupRuby();
private RubyUtil() {
}
/**
* Sets up the global {@link Ruby} runtime and ensures the creation of the "LogStash" module
* on it.
* @return Global {@link Ruby} Runtime
*/
private static Ruby setupRuby() {
final Ruby ruby = Ruby.getGlobalRuntime();
ruby.getOrCreateModule(LS_MODULE_NAME);
return ruby;
}
/**
* Wraps a Java exception in a JRuby IOError NativeException.
* This preserves the Java stacktrace and bubble up as a Ruby IOError
* @param runtime the Ruby runtime context
* @param e the Java exception to wrap
* @return RaiseException the wrapped IOError
*/
public static RaiseException newRubyIOError(Ruby runtime, Throwable e) {
// will preserve Java stacktrace & bubble up as a Ruby IOError
return new RaiseException(e, new NativeException(runtime, runtime.getIOError(), e));
}
}

View file

@ -12,11 +12,11 @@ import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.Library;
import org.logstash.RubyUtil;
import org.logstash.ackedqueue.Batch;
import org.logstash.Event;
import org.logstash.ackedqueue.Queueable;
import org.logstash.ext.JrubyEventExtLibrary;
import java.io.IOException;
public class JrubyAckedBatchExtLibrary implements Library {
@ -81,7 +81,7 @@ public class JrubyAckedBatchExtLibrary implements Library {
try {
this.batch.close();
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}
return context.nil;

View file

@ -14,6 +14,7 @@ import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.Library;
import org.logstash.RubyUtil;
import org.logstash.Event;
import org.logstash.ackedqueue.Batch;
import org.logstash.ackedqueue.Queue;
@ -130,7 +131,7 @@ public class JrubyAckedQueueExtLibrary implements Library {
try {
this.queue.open();
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}
return context.nil;
@ -147,7 +148,7 @@ public class JrubyAckedQueueExtLibrary implements Library {
try {
seqNum = this.queue.write(((JrubyEventExtLibrary.RubyEvent) event).getEvent());
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}
return context.runtime.newFixnum(seqNum);
@ -161,7 +162,7 @@ public class JrubyAckedQueueExtLibrary implements Library {
try {
b = this.queue.readBatch(RubyFixnum.num2int(limit), RubyFixnum.num2int(timeout));
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}
// TODO: return proper Batch object
@ -186,7 +187,7 @@ public class JrubyAckedQueueExtLibrary implements Library {
try {
this.queue.close();
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}
return context.nil;

View file

@ -14,6 +14,7 @@ import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.Library;
import org.logstash.RubyUtil;
import org.logstash.Event;
import org.logstash.ackedqueue.Batch;
import org.logstash.ackedqueue.Queue;
@ -127,7 +128,7 @@ public class JrubyAckedQueueMemoryExtLibrary implements Library {
this.queue.getCheckpointIO().purge();
this.queue.open();
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}
return context.nil;
@ -144,7 +145,7 @@ public class JrubyAckedQueueMemoryExtLibrary implements Library {
try {
seqNum = this.queue.write(((JrubyEventExtLibrary.RubyEvent) event).getEvent());
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}
return context.runtime.newFixnum(seqNum);
@ -158,7 +159,7 @@ public class JrubyAckedQueueMemoryExtLibrary implements Library {
try {
b = this.queue.readBatch(RubyFixnum.num2int(limit), RubyFixnum.num2int(timeout));
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}
// TODO: return proper Batch object
@ -183,7 +184,7 @@ public class JrubyAckedQueueMemoryExtLibrary implements Library {
try {
this.queue.close();
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}
return context.nil;