mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-10 09:15:47 +00:00
Throw an exception if command failed to execute with exit code
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Renci.SshClient.Common;
|
||||
using Renci.SshClient.Messages;
|
||||
using Renci.SshClient.Messages.Connection;
|
||||
|
||||
@@ -231,7 +232,12 @@ namespace Renci.SshClient.Channels
|
||||
ChannelNumber = message.ChannelNumber,
|
||||
};
|
||||
|
||||
// TODO: if exitStatus is not 0 then throw an exception or notify user that command failed to execute correctly
|
||||
// Throw an error if exit status is not 0
|
||||
if (exitStatus > 0)
|
||||
{
|
||||
throw new SshException(string.Format("Operation failed. Exit status: {0}", exitStatus), false);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -5,8 +5,6 @@ using Renci.SshClient.Messages.Connection;
|
||||
|
||||
namespace Renci.SshClient.Channels
|
||||
{
|
||||
// TODO: Add Begin* and End* methods for async calls
|
||||
|
||||
internal class ChannelExec : Channel
|
||||
{
|
||||
private Stream _channelData;
|
||||
|
||||
@@ -10,6 +10,8 @@ using Renci.SshClient.Messages.Sftp;
|
||||
|
||||
namespace Renci.SshClient.Channels
|
||||
{
|
||||
// TODO: Add Begin* and End* methods for async calls
|
||||
|
||||
internal class ChannelSftp : Channel
|
||||
{
|
||||
private EventWaitHandle _channelRequestSuccessWaitHandle = new AutoResetEvent(false);
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Renci.SshClient.Common
|
||||
{
|
||||
public class SshException : Exception
|
||||
{
|
||||
public bool ShouldDisconnect { get; private set; }
|
||||
|
||||
public SshException()
|
||||
{
|
||||
this.ShouldDisconnect = true;
|
||||
}
|
||||
|
||||
public SshException(string message)
|
||||
: base(message)
|
||||
{
|
||||
this.ShouldDisconnect = true;
|
||||
}
|
||||
|
||||
public SshException(string message, Exception inner)
|
||||
: base(message, inner)
|
||||
{
|
||||
this.ShouldDisconnect = true;
|
||||
}
|
||||
|
||||
public SshException(string message, bool shouldDisconnect)
|
||||
: base(message)
|
||||
{
|
||||
this.ShouldDisconnect = shouldDisconnect;
|
||||
}
|
||||
|
||||
public SshException(string message, bool shouldDisconnect, Exception inner)
|
||||
: base(message, inner)
|
||||
{
|
||||
this.ShouldDisconnect = shouldDisconnect;
|
||||
}
|
||||
|
||||
// This constructor is needed for serialization.
|
||||
protected SshException(SerializationInfo info, StreamingContext context)
|
||||
: base(info, context)
|
||||
{
|
||||
// Add implementation.
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,7 @@
|
||||
<Compile Include="Channels\ChannelAsyncResult.cs" />
|
||||
<Compile Include="Channels\ChannelEventArgs.cs" />
|
||||
<Compile Include="Common\BlockingStack.cs" />
|
||||
<Compile Include="Common\SshException.cs" />
|
||||
<Compile Include="Security\Algorithm.cs" />
|
||||
<Compile Include="Security\Cipher.cs" />
|
||||
<Compile Include="Security\CipherAES128.cs" />
|
||||
|
||||
@@ -914,9 +914,9 @@ namespace Renci.SshClient
|
||||
/// </summary>
|
||||
private void MessageListener()
|
||||
{
|
||||
try
|
||||
while (this._socket.Connected)
|
||||
{
|
||||
while (this._socket.Connected)
|
||||
try
|
||||
{
|
||||
dynamic message = this.ReceiveMessage();
|
||||
|
||||
@@ -931,30 +931,43 @@ namespace Renci.SshClient
|
||||
// Raise an event that message received
|
||||
this.RaiseMessageReceived(this, new MessageReceivedEventArgs(message));
|
||||
}
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
// TODO: This exception can be swolloed if it occures while running in the background, look for possible solutions
|
||||
|
||||
// Ignore this error since socket was disconected
|
||||
if (exp is SocketException && ((SocketException)exp).SocketErrorCode == SocketError.ConnectionAborted && this._isDisconnecting)
|
||||
catch (SshException exp)
|
||||
{
|
||||
// Do nothing since connection was disconnected by the client
|
||||
}
|
||||
else
|
||||
{
|
||||
// In case of error issue disconntect command
|
||||
this.Disconnect(DisconnectReasonCodes.ByApplication, exp.ToString());
|
||||
if (exp.ShouldDisconnect)
|
||||
{
|
||||
// In case of error issue disconntect command
|
||||
this.Disconnect(DisconnectReasonCodes.ByApplication, exp.ToString());
|
||||
}
|
||||
|
||||
this._exceptionToThrow = exp;
|
||||
|
||||
this._exceptionWaitHandle.Set();
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
// TODO: This exception can be swolloed if it occures while running in the background, look for possible solutions
|
||||
|
||||
// Ensure socket is disconnected
|
||||
this._socket.Close();
|
||||
// Ignore this error since socket was disconected
|
||||
if (exp is SocketException && ((SocketException)exp).SocketErrorCode == SocketError.ConnectionAborted && this._isDisconnecting)
|
||||
{
|
||||
// Do nothing since connection was disconnected by the client
|
||||
}
|
||||
else
|
||||
{
|
||||
// In case of error issue disconntect command
|
||||
this.Disconnect(DisconnectReasonCodes.ByApplication, exp.ToString());
|
||||
|
||||
this._exceptionToThrow = exp;
|
||||
|
||||
this._exceptionWaitHandle.Set();
|
||||
}
|
||||
|
||||
// Ensure socket is disconnected
|
||||
this._socket.Close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this._listenerWaitHandle.Set();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user