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
|
||||||
|
|
||||||
|
with_error_handling do
|
||||||
url = "#{Vagrant.server_url}/api/v1/authenticate" +
|
url = "#{Vagrant.server_url}/api/v1/authenticate" +
|
||||||
"?access_token=#{token}"
|
"?access_token=#{token}"
|
||||||
RestClient.get(url, content_type: :json)
|
RestClient.get(url, content_type: :json)
|
||||||
true
|
true
|
||||||
rescue RestClient::Unauthorized
|
end
|
||||||
false
|
|
||||||
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)
|
||||||
|
with_error_handling do
|
||||||
url = "#{Vagrant.server_url}/api/v1/authenticate"
|
url = "#{Vagrant.server_url}/api/v1/authenticate"
|
||||||
request = { "user" => { "login" => user, "password" => pass } }
|
request = { "user" => { "login" => user, "password" => pass } }
|
||||||
response = RestClient.post(
|
response = RestClient.post(
|
||||||
url, JSON.dump(request), content_type: :json)
|
url, JSON.dump(request), content_type: :json)
|
||||||
data = JSON.load(response.to_s)
|
data = JSON.load(response.to_s)
|
||||||
data["token"]
|
data["token"]
|
||||||
rescue RestClient::Unauthorized
|
end
|
||||||
return nil
|
|
||||||
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