Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No errors thrown in callbacks #63

Open
colinxs opened this issue Jan 30, 2019 · 2 comments
Open

No errors thrown in callbacks #63

colinxs opened this issue Jan 30, 2019 · 2 comments

Comments

@colinxs
Copy link

colinxs commented Jan 30, 2019

No errors are printed to stdout. Roslaunch is configured with output="screen". Here's an example (note the tools calls to error() in callback and main:

#!/usr/bin/env julia

using RobotOS
@rosimport geometry_msgs.msg: Point, Pose2D
rostypegen()
using geometry_msgs.msg

function callback(msg::Pose2D, pub_obj::Publisher{Point})
    pt_msg = Point(msg.x, msg.y, 0.0)
    error("this does not throw an error, and instead fails silently (we never reach the next line)")
    publish(pub_obj, pt_msg)
end

function loop(pub_obj)
    loop_rate = Rate(5.0)
    while ! is_shutdown()
        npt = Point(rand(), rand(), 0.0)
        publish(pub_obj, npt)
        rossleep(loop_rate)
    end
end

function main()
    init_node("rosjl_example")
    error("This correctly throws an error")
    pub = Publisher{Point}("pts", queue_size=10)
    sub = Subscriber{Pose2D}("pose", callback, (pub,), queue_size=10)
    loop(pub)
end

if ! isinteractive()
    main()
end

Is there any way to to have these errors raised so that the process exits? Having them fail silently makes debugging far more difficult. Thanks!

@schmrlng
Copy link
Contributor

The callbacks are run in a separate julia process which will die if there's an uncaught error. One possible solution is to try/catch any errors using standard Julia control flow (e.g., as done here) and do whatever logic you'd like (shutting down the program, printing the error and stack trace and continuing on, etc.).

@colinxs
Copy link
Author

colinxs commented Feb 19, 2019

Makes sense! I put together a little macro that has proved to be very useful if you (or anyone else) finds a need for it:

macro debugtask(ex)
  quote
    try
      $(esc(ex))
    catch e
      bt = stacktrace(catch_backtrace())
      io = IOBuffer()
      showerror(io, e, bt)
      errstr = String(take!(io))
      RobotOS.logfatal("Error: $errstr")
      exit()
    end
  end
end

function foo_callback(msg)
  @debugtask begin
     error("whoops")
  end
end

Now when the error is thrown it shuts down the node and causes the error message to be logged to stderr and /rosout as one would expect in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants