Selaa lähdekoodia

Use ExceptionDispatchInfo in more places (#1182)

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
Rob Hague 2 vuotta sitten
vanhempi
sitoutus
aade354748

+ 2 - 1
src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Linq;
+using System.Runtime.ExceptionServices;
 using System.Threading;
 
 using Renci.SshNet.Abstractions;
@@ -78,7 +79,7 @@ namespace Renci.SshNet
 
             if (_exception != null)
             {
-                throw _exception;
+                ExceptionDispatchInfo.Capture(_exception).Throw();
             }
 
             return _authenticationResult;

+ 2 - 1
src/Renci.SshNet/PasswordAuthenticationMethod.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Runtime.ExceptionServices;
 using System.Text;
 using System.Threading;
 
@@ -114,7 +115,7 @@ namespace Renci.SshNet
 
             if (_exception != null)
             {
-                throw _exception;
+                ExceptionDispatchInfo.Capture(_exception).Throw();
             }
 
             return _authenticationResult;

+ 3 - 2
src/Renci.SshNet/Sftp/SftpFileReader.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Globalization;
+using System.Runtime.ExceptionServices;
 using System.Threading;
 
 using Renci.SshNet.Abstractions;
@@ -80,7 +81,7 @@ namespace Renci.SshNet.Sftp
 
             if (_exception is not null)
             {
-                throw _exception;
+                ExceptionDispatchInfo.Capture(_exception).Throw();
             }
 
             if (_isEndOfFileRead)
@@ -102,7 +103,7 @@ namespace Renci.SshNet.Sftp
                 // throw when exception occured in read-ahead, or the current instance is already disposed
                 if (_exception != null)
                 {
-                    throw _exception;
+                    ExceptionDispatchInfo.Capture(_exception).Throw();
                 }
 
                 var data = nextChunk.Data;

+ 3 - 1
src/Renci.SshNet/SshCommand.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Globalization;
 using System.IO;
+using System.Runtime.ExceptionServices;
 using System.Text;
 using System.Threading;
 
@@ -483,7 +484,8 @@ namespace Renci.SshNet
             switch (signaledElement)
             {
                 case 0:
-                    throw _exception;
+                    ExceptionDispatchInfo.Capture(_exception).Throw();
+                    break;
                 case 1:
                     // Specified waithandle was signaled
                     break;

+ 9 - 4
src/Renci.SshNet/SubsystemSession.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Globalization;
+using System.Runtime.ExceptionServices;
 using System.Threading;
 
 using Renci.SshNet.Abstractions;
@@ -241,7 +242,8 @@ namespace Renci.SshNet
             switch (result)
             {
                 case 0:
-                    throw _exception;
+                    ExceptionDispatchInfo.Capture(_exception).Throw();
+                    break;
                 case 1:
                     throw new SshException("Connection was closed by the server.");
                 case 2:
@@ -286,7 +288,8 @@ namespace Renci.SshNet
             switch (result)
             {
                 case 0:
-                    throw _exception;
+                    ExceptionDispatchInfo.Capture(_exception).Throw();
+                    return false; // unreached
                 case 1:
                     throw new SshException("Connection was closed by the server.");
                 case 2:
@@ -340,7 +343,8 @@ namespace Renci.SshNet
             switch (result)
             {
                 case 0:
-                    throw _exception;
+                    ExceptionDispatchInfo.Capture(_exception).Throw();
+                    return -1; // unreached
                 case 1:
                     throw new SshException("Connection was closed by the server.");
                 case 2:
@@ -377,7 +381,8 @@ namespace Renci.SshNet
             switch (result)
             {
                 case 0:
-                    throw _exception;
+                    ExceptionDispatchInfo.Capture(_exception).Throw();
+                    return -1; // unreached
                 case 1:
                     throw new SshException("Connection was closed by the server.");
                 case 2: