Skip to content

Commit

Permalink
bubble up java stacktrace from wrapped exception
Browse files Browse the repository at this point in the history
move exception builder in RubyUtil

cosmetic

cosmetic
  • Loading branch information
colinsurprenant committed Sep 14, 2017
1 parent aa42302 commit 574ab6e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
14 changes: 14 additions & 0 deletions logstash-core/src/main/java/org/logstash/RubyUtil.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -30,4 +32,16 @@ private static Ruby setupRuby() {
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.logstash.Event;
import org.logstash.ackedqueue.Queueable;
import org.logstash.ext.JrubyEventExtLibrary;

import java.io.IOException;

public final class JrubyAckedBatchExtLibrary implements Library {
Expand Down Expand Up @@ -84,7 +83,7 @@ public IRubyObject ruby_close(ThreadContext context)
try {
this.batch.close();
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}

return context.nil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public IRubyObject ruby_open(ThreadContext context)
try {
this.queue.open();
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}

return context.nil;
Expand All @@ -145,7 +145,7 @@ public IRubyObject ruby_write(ThreadContext context, IRubyObject event)
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);
Expand All @@ -159,7 +159,7 @@ public IRubyObject ruby_read_batch(ThreadContext context, IRubyObject limit, IRu
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
Expand All @@ -184,7 +184,7 @@ public IRubyObject ruby_close(ThreadContext context)
try {
this.queue.close();
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}

return context.nil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public IRubyObject ruby_open(ThreadContext context)
this.queue.getCheckpointIO().purge();
this.queue.open();
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}

return context.nil;
Expand All @@ -143,7 +143,7 @@ public IRubyObject ruby_write(ThreadContext context, IRubyObject event)
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);
Expand All @@ -157,7 +157,7 @@ public IRubyObject ruby_read_batch(ThreadContext context, IRubyObject limit, IRu
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
Expand All @@ -182,7 +182,7 @@ public IRubyObject ruby_close(ThreadContext context)
try {
this.queue.close();
} catch (IOException e) {
throw context.runtime.newIOErrorFromException(e);
throw RubyUtil.newRubyIOError(context.runtime, e);
}

return context.nil;
Expand Down

0 comments on commit 574ab6e

Please sign in to comment.