Search code examples
ruby-on-railsrest-client

How to handle RestClient::ServerBrokeConnection


I am using the latest version of rest-client gem and upon external access I see a lots of RestClient::ServerBrokeConnection errors, how should I handle this?

The following call fails

response = RestClient::Request.execute(method: :post, url: url, headers: headers, "Content-Type" => "application/x-www-form-urlencoded")

Solution

  • This error happens when the server broke the connection with the client. You can decide to retry the request or just bubble the error for the user to know about it and handle it.

    Because how rest-client handles broken connections as shown here, all you can do is rescue from it

    begin
      response = RestClient::Request.execute(method: :post, url: url, headers: headers, "Content-Type" => "application/x-www-form-urlencoded")
    rescue RestClient::ServerBrokeConnection
      // retry or do something
    end