Skip to content

Commit

Permalink
Updating to use new error handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-ball committed Mar 29, 2018
1 parent 8fb6b2b commit 90629ef
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
15 changes: 10 additions & 5 deletions components/chef-workstation/i18n/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ commands:
the name of the user you wanted to create.
root_description: "Whether to use root permissions on the target. Defaults to true."
identity_file: "SSH identity file to use when connecting."
validation:
not_enough_params: You must supply <TARGET>, <RESOURCE> and <RESOURCE_NAME>
invalid_attribute: Attribute '%1' did not match the 'key=value' syntax required
status:
verifying: Verifying Chef client installation.
converging: Converging %1...
Expand Down Expand Up @@ -125,6 +122,16 @@ errors:
Available commands are: %2
# CLI Validation errors are prefixed with CHEFVAL
CHEFVAL001: |
No identity file at %1
CHEFVAL002: |
You must supply <TARGET>, <RESOURCE> and <RESOURCE_NAME>
CHEFVAL003: |
Attribute '%1' did not match the 'key=value' syntax required
# General errors/unknown errors are handled with CHEFINT
CHEFINT001: |
An unexpected error has occurred:
Expand Down Expand Up @@ -152,5 +159,3 @@ errors:
neither: |
If you are not able to resolve this issue, please contact Chef support
at support@chef.io.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require "chef-workstation/config"
require "chef-workstation/text"
require "chef-workstation/log"
require "chef-workstation/error"

module ChefWorkstation
module Command
Expand Down Expand Up @@ -128,6 +129,10 @@ def subcommands
@command_spec.subcommands
end

class OptionValidationError < ChefWorkstation::Error
def initialize(id, *args); super(id, *args); end
end

end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class Converge < ChefWorkstation::Command::Base
:short => "-i PATH",
:description => T.identity_file,
:proc => (Proc.new do |path|
raise "No identity file at #{path}" unless File.exist?(path)
unless File.exist?(path)
raise OptionValidationError.new("CHEFVAL001", path)
end
path
end)

Expand Down Expand Up @@ -72,12 +74,12 @@ def run(params)
ATTRIBUTE_MATCHER = /^([a-zA-Z0-9]+)=(\w+)$/
def validate_params(params)
if params.size < 3
raise T.validation.not_enough_params
raise OptionValidationError.new("CHEFVAL002")
end
attributes = params[3..-1]
attributes.each do |attribute|
unless attribute =~ ATTRIBUTE_MATCHER
raise T.validation.invalid_attribute(attribute)
raise OptionValidationError.new("CHEFVAL003", attribute)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,32 @@
subject(:cmd) do
ChefWorkstation::Command::Target::Converge.new(cmd_spec)
end
OptionValidationError = ChefWorkstation::Command::Target::Converge::OptionValidationError

describe "#validate_params" do
it "raises an error if not enough params are specified" do
expect { cmd.validate_params([]) }.to raise_error(/must supply/)
expect { cmd.validate_params(%w{one two}) }.to raise_error(/must supply/)
params = [
[],
%w{one two}
]
params.each do |p|
expect { cmd.validate_params(p) }.to raise_error(OptionValidationError) do |e|
e.id == "CHEFVAL002"
end
end
end

it "raises an error if attributes are not specified as key value pairs" do
expect { cmd.validate_params(%w{one two three four}) }.to raise_error(/four.+key=value/)
expect { cmd.validate_params(%w{one two three four=value five six=value}) }.to raise_error(/five.+key=value/)
expect { cmd.validate_params(%w{one two three non.word=value}) }.to raise_error(/non\.word.+key=value/)
params = [
%w{one two three four},
%w{one two three four=value five six=value},
%w{one two three non.word=value},
]
params.each do |p|
expect { cmd.validate_params(p) }.to raise_error(OptionValidationError) do |e|
e.id == "CHEFVAL003"
end
end
end
end

Expand Down

0 comments on commit 90629ef

Please sign in to comment.