From 62065e013a928772b9ccb8e594056f096bbd182d Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 8 Dec 2014 18:24:01 -0800 Subject: [PATCH] Read and return login errors from Atlas --- plugins/commands/login/client.rb | 50 +++++++++++++++++---------- plugins/commands/login/errors.rb | 4 +++ plugins/commands/login/locales/en.yml | 4 +++ 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/plugins/commands/login/client.rb b/plugins/commands/login/client.rb index 01ba7649a..f06772be9 100644 --- a/plugins/commands/login/client.rb +++ b/plugins/commands/login/client.rb @@ -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 diff --git a/plugins/commands/login/errors.rb b/plugins/commands/login/errors.rb index a6888b597..614c37cf6 100644 --- a/plugins/commands/login/errors.rb +++ b/plugins/commands/login/errors.rb @@ -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 diff --git a/plugins/commands/login/locales/en.yml b/plugins/commands/login/locales/en.yml index b62b24196..51020df1d 100644 --- a/plugins/commands/login/locales/en.yml +++ b/plugins/commands/login/locales/en.yml @@ -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.