Read and return login errors from Atlas
This commit is contained in:
parent
3d8a1ec3fc
commit
62065e013a
|
@ -23,14 +23,12 @@ module VagrantPlugins
|
||||||
token = self.token
|
token = self.token
|
||||||
return false if !token
|
return false if !token
|
||||||
|
|
||||||
url = "#{Vagrant.server_url}/api/v1/authenticate" +
|
with_error_handling do
|
||||||
"?access_token=#{token}"
|
url = "#{Vagrant.server_url}/api/v1/authenticate" +
|
||||||
RestClient.get(url, content_type: :json)
|
"?access_token=#{token}"
|
||||||
true
|
RestClient.get(url, content_type: :json)
|
||||||
rescue RestClient::Unauthorized
|
true
|
||||||
false
|
end
|
||||||
rescue SocketError
|
|
||||||
raise Errors::ServerUnreachable, url: Vagrant.server_url.to_s
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Login logs a user in and returns the token for that user. The token
|
# Login logs a user in and returns the token for that user. The token
|
||||||
|
@ -40,16 +38,14 @@ module VagrantPlugins
|
||||||
# @param [String] pass
|
# @param [String] pass
|
||||||
# @return [String] token The access token, or nil if auth failed.
|
# @return [String] token The access token, or nil if auth failed.
|
||||||
def login(user, pass)
|
def login(user, pass)
|
||||||
url = "#{Vagrant.server_url}/api/v1/authenticate"
|
with_error_handling do
|
||||||
request = { "user" => { "login" => user, "password" => pass } }
|
url = "#{Vagrant.server_url}/api/v1/authenticate"
|
||||||
response = RestClient.post(
|
request = { "user" => { "login" => user, "password" => pass } }
|
||||||
url, JSON.dump(request), content_type: :json)
|
response = RestClient.post(
|
||||||
data = JSON.load(response.to_s)
|
url, JSON.dump(request), content_type: :json)
|
||||||
data["token"]
|
data = JSON.load(response.to_s)
|
||||||
rescue RestClient::Unauthorized
|
data["token"]
|
||||||
return nil
|
end
|
||||||
rescue SocketError
|
|
||||||
raise Errors::ServerUnreachable, url: Vagrant.server_url.to_s
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Stores the given token locally, removing any previous tokens.
|
# Stores the given token locally, removing any previous tokens.
|
||||||
|
@ -73,6 +69,24 @@ module VagrantPlugins
|
||||||
|
|
||||||
protected
|
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
|
def token_path
|
||||||
@env.data_dir.join("vagrant_login_token")
|
@env.data_dir.join("vagrant_login_token")
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,6 +5,10 @@ module VagrantPlugins
|
||||||
error_namespace("login_command.errors")
|
error_namespace("login_command.errors")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class ServerError < Error
|
||||||
|
error_key(:server_error)
|
||||||
|
end
|
||||||
|
|
||||||
class ServerUnreachable < Error
|
class ServerUnreachable < Error
|
||||||
error_key(:server_unreachable)
|
error_key(:server_unreachable)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
en:
|
en:
|
||||||
login_command:
|
login_command:
|
||||||
errors:
|
errors:
|
||||||
|
server_error: |-
|
||||||
|
The Atlas server responded with an not-OK response:
|
||||||
|
|
||||||
|
%{errors}
|
||||||
server_unreachable: |-
|
server_unreachable: |-
|
||||||
The Atlas server is not currently accepting connections. Please check
|
The Atlas server is not currently accepting connections. Please check
|
||||||
your network connection and try again later.
|
your network connection and try again later.
|
||||||
|
|
Loading…
Reference in New Issue