1

When i try in postman for the user authentication, it works perfectly.

Request:

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

Response:

{
  "token": "some-token"
}

But when i try to create a test login. I get ActiveRestClient::HTTPServerException: ActiveRestClient::HTTPServerException

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

Why am i getting ActiveRestClient::HTTPServerException?.

Taryn East
  • 27,486
  • 9
  • 86
  • 108
Frank
  • 179
  • 2
  • 10

1 Answers1

2

ActiveRestClient documentation recommends to capture this kind of errors to check the server response:

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

If the API response is an error (4xx or 5xx) you'll be able to check it on your Rails log

fjuan
  • 2,214
  • 2
  • 17
  • 20
  • Thanks, i did not try your way... but why do we need to use begin and rescue.... and more over will the code fail if i don't use begin and rescue... – Frank Aug 06 '14 at 17:59
  • It's because the active-rest-client gem raises this kind of errors when the server you're trying to connect to responds with http errors – fjuan Aug 06 '14 at 18:03
  • Short answer: it will work. Long: if the request gets an http error, for example 401 Forbidden, ActiveRestClient will raise an error and your code will stop executing. I'd recommend rescuing it for better handling your program flow and help your users to find a solution – fjuan Aug 06 '14 at 18:18