Discussion:
[jruby-user] Convert Ruby Exceptions to Java
Ariel Valentin
2014-08-04 13:40:59 UTC
Permalink
Is anyone aware of a convenient way to convert Ruby exceptions into Java
Throwable? I am trying to use an slf4j logger to log Ruby errors.

Thanks,
Ariel Valentin
e-mail: ***@arielvalentin.com
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Theo Hultberg
2014-08-04 20:24:45 UTC
Permalink
when Ruby exception bubble into Java they are wrapped in instances of
RaiseException, a subclass of RuntimeException. The constructor of
RaiseException takes a RubyException so they should do the job:

https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/exceptions/RaiseException.java

T#
Post by Ariel Valentin
Is anyone aware of a convenient way to convert Ruby exceptions into Java
Throwable? I am trying to use an slf4j logger to log Ruby errors.
Thanks,
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Ariel Valentin
2014-08-05 03:20:50 UTC
Permalink
The code is still executing in the Ruby context. e.g.
https://gist.github.com/arielvalentin/c340ae45dd83af8fd9d9

Ariel Valentin
e-mail: ***@arielvalentin.com
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Post by Theo Hultberg
when Ruby exception bubble into Java they are wrapped in instances of
RaiseException, a subclass of RuntimeException. The constructor of
https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/exceptions/RaiseException.java
T#
Post by Ariel Valentin
Is anyone aware of a convenient way to convert Ruby exceptions into Java
Throwable? I am trying to use an slf4j logger to log Ruby errors.
Thanks,
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Karol Bucek
2014-08-05 05:32:00 UTC
Permalink
you can still emulate ~ wrapping ... if you've looked at the linked
RaiseException.java there's a constructor that takes a RubyException :

begin
raise "error"
rescue => e
t = org.jruby.exceptions.RaiseException.new(e)
logger.error("This doesn't ... but will !", t)
end

although this might not be ideal, I would consider adding a "Ruby" logging
method (on each level) to the logger's class that would format errors with
their back-traces

K.
Post by Ariel Valentin
The code is still executing in the Ruby context. e.g.
https://gist.github.com/arielvalentin/c340ae45dd83af8fd9d9
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Post by Theo Hultberg
when Ruby exception bubble into Java they are wrapped in instances of
RaiseException, a subclass of RuntimeException. The constructor of
https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/exceptions/RaiseException.java
T#
Post by Ariel Valentin
Is anyone aware of a convenient way to convert Ruby exceptions into Java
Throwable? I am trying to use an slf4j logger to log Ruby errors.
Thanks,
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Ariel Valentin
2014-08-05 10:37:01 UTC
Permalink
It occurred to me that Ruby loggers don't have the same error printing features of SLF4J, so I think I am heading the route of using a utility method to do that and just use error(String) for both cases.

Thanks,
Ariel
---
Sent from my mobile device. Please excuse any errors.
Post by Karol Bucek
begin
raise "error"
rescue => e
t = org.jruby.exceptions.RaiseException.new(e)
logger.error("This doesn't ... but will !", t)
end
although this might not be ideal, I would consider adding a "Ruby" logging method (on each level) to the logger's class that would format errors with their back-traces
K.
The code is still executing in the Ruby context. e.g. https://gist.github.com/arielvalentin/c340ae45dd83af8fd9d9
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Post by Theo Hultberg
https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/exceptions/RaiseException.java
T#
Is anyone aware of a convenient way to convert Ruby exceptions into Java Throwable? I am trying to use an slf4j logger to log Ruby errors.
Thanks,
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Theo Hultberg
2014-08-06 15:29:36 UTC
Permalink
btw. if you haven't already written all your slf4j-integration code I built
a Ruby Logger to SLF4J bridge a few months ago:
https://github.com/iconara/kreps

it doesn't have any exception logging feature though, since, as you say,
Ruby's Logger doesn't have it. Maybe it wouldn't be a bad feature to pass
Ruby exceptions to SLF4J if you pass an exception object instead of a
string as message.

T#
Post by Ariel Valentin
It occurred to me that Ruby loggers don't have the same error printing
features of SLF4J, so I think I am heading the route of using a utility
method to do that and just use error(String) for both cases.
Thanks,
Ariel
---
Sent from my mobile device. Please excuse any errors.
you can still emulate ~ wrapping ... if you've looked at the linked
begin
raise "error"
rescue => e
t = org.jruby.exceptions.RaiseException.new(e)
logger.error("This doesn't ... but will !", t)
end
although this might not be ideal, I would consider adding a "Ruby" logging
method (on each level) to the logger's class that would format errors with
their back-traces
K.
Post by Ariel Valentin
The code is still executing in the Ruby context. e.g.
https://gist.github.com/arielvalentin/c340ae45dd83af8fd9d9
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Post by Theo Hultberg
when Ruby exception bubble into Java they are wrapped in instances of
RaiseException, a subclass of RuntimeException. The constructor of
https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/exceptions/RaiseException.java
T#
Post by Ariel Valentin
Is anyone aware of a convenient way to convert Ruby exceptions into
Java Throwable? I am trying to use an slf4j logger to log Ruby errors.
Thanks,
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Lenny Marks
2014-08-06 15:56:43 UTC
Permalink
You can also check out log4jruby (https://github.com/lenny/log4jruby). We've been using it in production for quite some time now for our Rails/Warbler/Tomcat apps.

-lenny
btw. if you haven't already written all your slf4j-integration code I built a Ruby Logger to SLF4J bridge a few months ago: https://github.com/iconara/kreps
it doesn't have any exception logging feature though, since, as you say, Ruby's Logger doesn't have it. Maybe it wouldn't be a bad feature to pass Ruby exceptions to SLF4J if you pass an exception object instead of a string as message.
T#
It occurred to me that Ruby loggers don't have the same error printing features of SLF4J, so I think I am heading the route of using a utility method to do that and just use error(String) for both cases.
Thanks,
Ariel
---
Sent from my mobile device. Please excuse any errors.
Post by Karol Bucek
begin
raise "error"
rescue => e
t = org.jruby.exceptions.RaiseException.new(e)
logger.error("This doesn't ... but will !", t)
end
although this might not be ideal, I would consider adding a "Ruby" logging method (on each level) to the logger's class that would format errors with their back-traces
K.
The code is still executing in the Ruby context. e.g. https://gist.github.com/arielvalentin/c340ae45dd83af8fd9d9
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/exceptions/RaiseException.java
T#
Is anyone aware of a convenient way to convert Ruby exceptions into Java Throwable? I am trying to use an slf4j logger to log Ruby errors.
Thanks,
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Ariel Valentin
2014-08-06 16:15:38 UTC
Permalink
Thanks all! I will look into these options.

Thanks,
Ariel
---
Sent from my mobile device. Please excuse any errors.
Post by Lenny Marks
You can also check out log4jruby (https://github.com/lenny/log4jruby). We've been using it in production for quite some time now for our Rails/Warbler/Tomcat apps.
-lenny
btw. if you haven't already written all your slf4j-integration code I built a Ruby Logger to SLF4J bridge a few months ago: https://github.com/iconara/kreps
it doesn't have any exception logging feature though, since, as you say, Ruby's Logger doesn't have it. Maybe it wouldn't be a bad feature to pass Ruby exceptions to SLF4J if you pass an exception object instead of a string as message.
T#
Post by Ariel Valentin
It occurred to me that Ruby loggers don't have the same error printing features of SLF4J, so I think I am heading the route of using a utility method to do that and just use error(String) for both cases.
Thanks,
Ariel
---
Sent from my mobile device. Please excuse any errors.
Post by Karol Bucek
begin
raise "error"
rescue => e
t = org.jruby.exceptions.RaiseException.new(e)
logger.error("This doesn't ... but will !", t)
end
although this might not be ideal, I would consider adding a "Ruby" logging method (on each level) to the logger's class that would format errors with their back-traces
K.
The code is still executing in the Ruby context. e.g. https://gist.github.com/arielvalentin/c340ae45dd83af8fd9d9
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Post by Theo Hultberg
https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/exceptions/RaiseException.java
T#
Is anyone aware of a convenient way to convert Ruby exceptions into Java Throwable? I am trying to use an slf4j logger to log Ruby errors.
Thanks,
Ariel Valentin
website: http://blog.arielvalentin.com
skype: ariel.s.valentin
twitter: arielvalentin
linkedin: http://www.linkedin.com/profile/view?id=8996534
---------------------------------------
*simplicity *communication
*feedback *courage *respect
Loading...