views:

63

answers:

2

I have a model


class Car
  @@RPCServer = XMLRPC::Client.new("localhost", "/", 8080)

  def self.count
    @@RPCServer.call("cars.count")
  end
end

If server is not running on localhost:8080 I've got a Errno::ECONNREFUSED error.
I want to display an error message to user, how can a handle this error?

+1  A: 

You need to trap the error in order to handle the exception in the way your application needs. The following code will trap this Exception. If you need to trap other exceptions then you can include multiple rescue clauses.

class Car
  @@RPCServer = XMLRPC::Client.new("localhost", "/", 8080)

  def self.count
    begin
      @@RPCServer.call("cars.count")
    rescue Errno::ECONNREFUSED
      # Do Appropriate handling here
    end
  end
end
Steve Weet
A: 

Thanks! Can I handle this error in one place, for example:


class Car
  begin
    @@RPCServer = XMLRPC::Client.new("localhost", "/", 8080)
  rescue Errno::ECONNREFUSED
    # Do Appropriate handling here
  end

  def self.count
    @@RPCServer.call("cars.count")
  end
end

Why it is not working? And how can a I pass an error to controller? How can I handle it in controller and view? Can I terminate execution of the controller in model and simply output the string to the browser?

stel