From e33b67e2ff09f6bb47162baa537411db99c1fa02 Mon Sep 17 00:00:00 2001 From: olegkap_cp Date: Wed, 20 Oct 2010 14:03:58 +0000 Subject: [PATCH] Add more information to SshException when command execuetd with error. Pass the error description to the exception. --- .../Renci.SshClient/Channels/Channel.cs | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/Renci.SshClient/Renci.SshClient/Channels/Channel.cs b/Renci.SshClient/Renci.SshClient/Channels/Channel.cs index 50cb8972..9455dbf9 100644 --- a/Renci.SshClient/Renci.SshClient/Channels/Channel.cs +++ b/Renci.SshClient/Renci.SshClient/Channels/Channel.cs @@ -1,4 +1,5 @@ using System; +using System.Text; using System.Threading; using Renci.SshClient.Common; using Renci.SshClient.Messages; @@ -28,6 +29,11 @@ namespace Renci.SshClient.Channels /// private bool _closeMessageSent = false; + /// + /// If error occured holds error message + /// + private StringBuilder _errorMessage; + public abstract ChannelTypes ChannelType { get; } public uint ClientChannelNumber { get; private set; } @@ -42,6 +48,8 @@ namespace Renci.SshClient.Channels public bool IsOpen { get; private set; } + public uint ExitStatus { get; private set; } + public event EventHandler Opened; public event EventHandler Closed; @@ -115,6 +123,12 @@ namespace Renci.SshClient.Channels protected virtual void OnChannelExtendedData(string data, uint dataTypeCode) { + if (dataTypeCode == 1) + { + if (this._errorMessage == null) + this._errorMessage = new StringBuilder(); + this._errorMessage.Append(data); + } } protected virtual void OnChannelSuccess() @@ -127,6 +141,11 @@ namespace Renci.SshClient.Channels protected virtual void OnChannelClose() { + // Throw an error if exit status is not 0 + if (this.ExitStatus > 0) + { + throw new SshException(this._errorMessage.ToString(), this.ExitStatus); + } } protected virtual void OnChannelFailed(uint reasonCode, string description) @@ -228,19 +247,12 @@ namespace Renci.SshClient.Channels if (message.RequestName == ChannelRequestNames.ExitStatus) { - var exitStatus = message.ExitStatus; + this.ExitStatus = message.ExitStatus; replyMessage = new ChannelSuccessMessage() { ChannelNumber = message.ChannelNumber, }; - - // Throw an error if exit status is not 0 - if (exitStatus > 0) - { - throw new SshException(string.Format("Operation failed. Exit status: {0}", exitStatus), exitStatus); - } - } else {