简体   繁体   中英

How to handle XMLRPC::FaultException errors?

I have a list of pingbacks that need to be sent for each post. It looks something like this:

def send_trackback
  #do stuff
  begin
    Pingback::Client.new.ping(source_uri, target_uri)
    trackback.status = 1
    trackback.save
  rescue
    trackback.status = 2
    trackback.save
    nil
  end
end

Here is the full error:

  XMLRPC::FaultException (The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.):
  app/controllers/posts_controller.rb:67:in `block in send_trackback'
  app/controllers/posts_controller.rb:62:in `send_trackback'
  app/controllers/posts_controller.rb:8:in `pingback'

Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (4.6ms)

Status starts out as being 0, but if the ping back fails that's okay but I want status set 2 and I want it just to keep going not throw an error to rails. If it is a success, I want status set to 1.

Shouldn't the begin - rescue blocks handle this? What am I missing?

That should work. It looks like the exception is getting triggered in pingback/send_trackback and not send_pingback

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM