From c62c1c7083718f34f7e64478ada4bbd405af49fb Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sat, 24 Sep 2016 13:29:43 -0700 Subject: [PATCH] (#33) Handle more exceptions in vmfloaty command class This commit adds more handling to the vmfloaty command class that deals with exceptions thrown from the Pooler class. --- lib/vmfloaty.rb | 34 +++++++++++++++++++++++++--------- lib/vmfloaty/errors.rb | 6 ++++++ lib/vmfloaty/pooler.rb | 5 ++++- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index b182ca1..3f9ec26 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -45,7 +45,15 @@ class Vmfloaty unless os_types.empty? if no_token - response = Pooler.retrieve(verbose, os_types, nil, url) + begin + response = Pooler.retrieve(verbose, os_types, nil, url) + rescue MissingParamError + STDERR.puts e + STDERR.puts "See `floaty get --help` for more information on how to get VMs." + rescue AuthError => e + STDERR.puts e + exit 1 + end puts Utils.format_hosts(response) exit 0 else @@ -58,7 +66,7 @@ class Vmfloaty pass = password "Enter your password please:", '*' begin token = Auth.get_token(verbose, url, user, pass) - rescue => e + rescue AuthError => e STDERR.puts e exit 1 end @@ -67,7 +75,15 @@ class Vmfloaty puts token end - response = Pooler.retrieve(verbose, os_types, token, url) + begin + response = Pooler.retrieve(verbose, os_types, token, url) + rescue MissingParamError + STDERR.puts e + STDERR.puts "See `floaty get --help` for more information on how to get VMs." + rescue AuthError => e + STDERR.puts e + exit 1 + end puts Utils.format_hosts(response) exit 0 end @@ -98,7 +114,7 @@ class Vmfloaty # list active vms begin status = Auth.token_status(verbose, url, token) - rescue => e + rescue AuthError => e STDERR.puts e exit 1 end @@ -208,7 +224,7 @@ class Vmfloaty # get vms with token begin status = Auth.token_status(verbose, url, token) - rescue => e + rescue AuthError => e STDERR.puts e exit 1 end @@ -357,7 +373,7 @@ class Vmfloaty pass = password "Enter your password please:", '*' begin token = Auth.get_token(verbose, url, user, pass) - rescue => e + rescue AuthError => e STDERR.puts e exit 1 end @@ -367,7 +383,7 @@ class Vmfloaty pass = password "Enter your password please:", '*' begin result = Auth.delete_token(verbose, url, user, pass, token) - rescue => e + rescue AuthError => e STDERR.puts e exit 1 end @@ -376,7 +392,7 @@ class Vmfloaty when "status" begin status = Auth.token_status(verbose, url, token) - rescue => e + rescue AuthError => e STDERR.puts e exit 1 end @@ -423,7 +439,7 @@ class Vmfloaty pass = password "Enter your password please:", '*' begin token = Auth.get_token(verbose, url, user, pass) - rescue => e + rescue AuthError => e STDERR.puts e STDERR.puts 'Could not get token...requesting vm without a token anyway...' else diff --git a/lib/vmfloaty/errors.rb b/lib/vmfloaty/errors.rb index 03e7861..aa3b2b8 100644 --- a/lib/vmfloaty/errors.rb +++ b/lib/vmfloaty/errors.rb @@ -9,3 +9,9 @@ class TokenError < StandardError super end end + +class MissingParamError < StandardError + def initialize(msg="Argument provided to function is missing") + super + end +end diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index f633846..9454ee0 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -20,6 +20,9 @@ class Pooler end def self.retrieve(verbose, os_type, token, url) + # NOTE: + # Developers can use `Utils.generate_os_hash` to + # generate the os_type param. conn = Http.get_conn(verbose, url) if token conn.headers['X-AUTH-TOKEN'] = token @@ -35,7 +38,7 @@ class Pooler os_string = os_string.chomp("+") if os_string.size == 0 - raise "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs." + raise MissingParamError, "No operating systems provided to obtain." end response = conn.post "vm/#{os_string}"