Read and return login errors from Atlas

This commit is contained in:
Seth Vargo 2014-12-08 18:24:01 -08:00
parent 3d8a1ec3fc
commit 62065e013a
3 changed files with 40 additions and 18 deletions

View File

@ -23,14 +23,12 @@ module VagrantPlugins
token = self.token
return false if !token
url = "#{Vagrant.server_url}/api/v1/authenticate" +
"?access_token=#{token}"
RestClient.get(url, content_type: :json)
true
rescue RestClient::Unauthorized
false
rescue SocketError
raise Errors::ServerUnreachable, url: Vagrant.server_url.to_s
with_error_handling do
url = "#{Vagrant.server_url}/api/v1/authenticate" +
"?access_token=#{token}"
RestClient.get(url, content_type: :json)
true
end
end
# Login logs a user in and returns the token for that user. The token
@ -40,16 +38,14 @@ module VagrantPlugins
# @param [String] pass
# @return [String] token The access token, or nil if auth failed.
def login(user, pass)
url = "#{Vagrant.server_url}/api/v1/authenticate"
request = { "user" => { "login" => user, "password" => pass } }
response = RestClient.post(
url, JSON.dump(request), content_type: :json)
data = JSON.load(response.to_s)
data["token"]
rescue RestClient::Unauthorized
return nil
rescue SocketError
raise Errors::ServerUnreachable, url: Vagrant.server_url.to_s
with_error_handling do
url = "#{Vagrant.server_url}/api/v1/authenticate"
request = { "user" => { "login" => user, "password" => pass } }
response = RestClient.post(
url, JSON.dump(request), content_type: :json)
data = JSON.load(response.to_s)
data["token"]
end
end
# Stores the given token locally, removing any previous tokens.
@ -73,6 +69,24 @@ module VagrantPlugins
protected
def with_error_handling(&block)
yield
rescue RestClient::Unauthorized
false
rescue RestClient::NotAcceptable => e
begin
errors = JSON.parse(e.response)["errors"]
.map { |h| h["message"] }
.join("\n")
raise Errors::ServerError, errors: errors
rescue JSON::ParserError; end
raise "An unexpected error occurred: #{e.inspect}"
rescue SocketError
raise Errors::ServerUnreachable, url: Vagrant.server_url.to_s
end
def token_path
@env.data_dir.join("vagrant_login_token")
end

View File

@ -5,6 +5,10 @@ module VagrantPlugins
error_namespace("login_command.errors")
end
class ServerError < Error
error_key(:server_error)
end
class ServerUnreachable < Error
error_key(:server_unreachable)
end

View File

@ -1,6 +1,10 @@
en:
login_command:
errors:
server_error: |-
The Atlas server responded with an not-OK response:
%{errors}
server_unreachable: |-
The Atlas server is not currently accepting connections. Please check
your network connection and try again later.