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 1340f2eee0
commit be20242d6f
4 changed files with 23 additions and 10 deletions

View file

@ -1,6 +1,8 @@
package org.logstash;
import org.jruby.NativeException;
import org.jruby.Ruby;
import org.jruby.exceptions.RaiseException;
/**
* Utilities around interaction with the {@link Ruby} runtime.
@ -30,4 +32,16 @@ public final class RubyUtil {
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

@ -17,7 +17,6 @@ import org.logstash.ackedqueue.Batch;
import org.logstash.Event;
import org.logstash.ackedqueue.Queueable;
import org.logstash.ext.JrubyEventExtLibrary;
import java.io.IOException;
public final class JrubyAckedBatchExtLibrary implements Library {
@ -84,7 +83,7 @@ public final 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

@ -128,7 +128,7 @@ public final 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;
@ -145,7 +145,7 @@ public final 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);
@ -159,7 +159,7 @@ public final 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
@ -184,7 +184,7 @@ public final 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

@ -126,7 +126,7 @@ public final 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;
@ -143,7 +143,7 @@ public final 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);
@ -157,7 +157,7 @@ public final 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
@ -182,7 +182,7 @@ public final 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;