【问题标题】:ActiveRestClient::HTTPServerException on user Authentication test用户身份验证测试中的 ActiveRestClient::HTTPServerException
【发布时间】:2014-08-06 16:59:43
【问题描述】:

当我为用户authentication 尝试使用postman 时,它运行良好。

请求:

{"email" : "shane@shane.com", "password": "123"}

回复:

{
  "token": "some-token"
}

但是当我尝试创建一个测试登录时。我得到ActiveRestClient::HTTPServerException: ActiveRestClient::HTTPServerException

@login = User.login(FactoryGirl.create(:user, 
          email: "shane@shane.com", password: "123"));

为什么我得到 ActiveRestClient::HTTPServerException?。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 rspec active-rest-client


    【解决方案1】:

    ActiveRestClient documentation 建议捕获此类错误以检查服务器响应:

    begin
      User.login(FactoryGirl.create(:user, email: "shane@shane.com", password: "123"))
    rescue ActiveRestClient::HTTPClientException, ActiveRestClient::HTTPServerException => e
      Rails.logger.error("API returned #{e.status} : #{e.result.message}")
    end
    

    如果 API 响应是错误(4xx 或 5xx),您可以在 Rails 日志中检查它

    【讨论】:

    • 谢谢,我没有尝试你的方式...但是为什么我们需要使用开始和救援...。如果我不使用开始和救援,代码会失败。 ..
    • 这是因为当您尝试连接的服务器响应 http 错误时,active-rest-client gem 会引发此类错误
    • 简短回答:它会起作用。 Long:如果请求收到 http 错误,例如 401 Forbidden,ActiveRestClient 将引发错误,您的代码将停止执​​行。我建议拯救它以更好地处理您的程序流程并帮助您的用户找到解决方案
    猜你喜欢
    • 2013-02-04
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    相关资源
    最近更新 更多