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

How to catch errors? #148

Closed
doits opened this issue Jul 14, 2015 · 2 comments
Closed

How to catch errors? #148

doits opened this issue Jul 14, 2015 · 2 comments

Comments

@doits
Copy link

doits commented Jul 14, 2015

I'm trying to catch errors from blather. I'm totally new to event machine and thread programming in ruby, so maybe this looks silly, but ...

I've created a blather client successfully like this:

# client.rb

require 'eventmachine' 
require 'blather/client/dsl'

class Client
  include Blather::DSL

  def initialize
    setup 'jabber@id/name', 'password'
  end

  def connect
    Thread.new do
      EM.run { client.connect }
    end
  end

  def connected?
    client.connected?
  end
end

client = Client.new
client.connect

until client.connected?
  puts 'Waiting for connection ...'
  sleep 1
end

client.say 'some@jid', 'hello!'
sleep 1000

This works good so far.

I can now trigger the error Stream Error (conflict): Replaced by new connection by simply running this script twice:

ruby client.rb & ruby client.rb

This error is OK of course, but how can I catch it so I can handle it in a custom way? I tried to rescue it everywhere without success, so it looks like this now:

  def connect
    begin
      Thread.new do
        begin
          EM.error_handler { puts 'rescued?' }
          EM.run do
            begin
              client.connect
            rescue
              puts 'rescued?'
            end
          end
        rescue
          puts 'rescued?'
        end
      end
    rescue
      puts 'rescued?'
    end
  end

But the error is never catched, even with this connect monstrosity above. What am I missing?

@benlangfeld
Copy link
Member

Thanks for asking. I'm going to close this as a duplicate of #73.

@doits
Copy link
Author

doits commented Jul 15, 2015

I'm not sure if this is exactly the same, because overwriting unbind as in #73 didn't work for me, but I managed to register a custom error handler which at least gets called additionally like this:

class Blather::Client
  attr_accessor :custom_error_handler

  alias_method :setup_initial_handlers_orig, :setup_initial_handlers
  def setup_initial_handlers
    register_handler :error do |err|
      custom_error_handler.call(err) if custom_error_handler
    end

    setup_initial_handlers_orig
  end
end

class Client
  include Blather::DSL

  def initialize
    client.custom_error_handler = proc do |err|
      # will get called on error like StreamError
    end
  end
end

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