Better WinRM command failure messaging

Command failures include the stdout and stderr in the error message just like the SSH communicator.
Its now possible to specify only an error_class and have that use the correct error_key by default.
This commit is contained in:
Shawn Neal 2014-06-24 10:09:11 -07:00
parent 1e28f1ac31
commit c72a412600
4 changed files with 33 additions and 8 deletions

View File

@ -60,8 +60,8 @@ module VagrantPlugins
command: command,
elevated: false,
error_check: true,
error_class: Errors::ExecutionError,
error_key: :execution_error,
error_class: Errors::WinRMBadExitStatus,
error_key: nil, # use the error_class message key
good_exit: 0,
shell: :powershell,
}.merge(opts || {})
@ -154,10 +154,17 @@ module VagrantPlugins
end
def raise_execution_error(output, opts)
# The error classes expect the translation key to be _key, but that makes for an ugly
# configuration parameter, so we set it here from `error_key`
msg = "Command execution failed with an exit code of #{output[:exitcode]}"
error_opts = opts.merge(_key: opts[:error_key], message: msg)
# WinRM can return multiple stderr and stdout entries
error_opts = opts.merge(
stdout: output[:data].collect { |e| e[:stdout] }.join,
stderr: output[:data].collect { |e| e[:stderr] }.join
)
# Use a different error message key if the caller gave us one,
# otherwise use the error's default message
error_opts[:_key] = opts[:error_key] if opts[:error_key]
# Raise the error, use the type the caller gave us or the comm default
raise opts[:error_class], error_opts
end
end #WinRM class

View File

@ -18,6 +18,10 @@ module VagrantPlugins
error_key(:invalid_shell)
end
class WinRMBadExitStatus < WinRMError
error_key(:winrm_bad_exit_status)
end
class WinRMNotReady < WinRMError
error_key(:winrm_not_ready)
end

View File

@ -8,6 +8,19 @@ en:
Password: %{password}
Endpoint: %{endpoint}
Message: %{message}
winrm_bad_exit_status: |-
The following WinRM command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
%{command}
Stdout from the command:
%{stdout}
Stderr from the command:
%{stderr}
execution_error: |-
An error occurred executing a remote WinRM command.

View File

@ -61,9 +61,10 @@ describe VagrantPlugins::CommunicatorWinRM::Communicator do
end
it "raises error when error_check is true and exit code is non-zero" do
expect(shell).to receive(:powershell).with(kind_of(String)).and_return({ exitcode: 1 })
expect(shell).to receive(:powershell).with(kind_of(String)).and_return(
{ exitcode: 1, data: [{ stdout: '', stderr: '' }] })
expect { subject.execute("dir") }.to raise_error(
VagrantPlugins::CommunicatorWinRM::Errors::ExecutionError)
VagrantPlugins::CommunicatorWinRM::Errors::WinRMBadExitStatus)
end
it "does not raise error when error_check is false and exit code is non-zero" do