@@ -81,6 +81,7 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor,
81
81
82
82
public void Run ( )
83
83
{
84
+ Exception thrownException = null ;
84
85
if ( Process . StartInfo . RedirectStandardError )
85
86
{
86
87
Process . ErrorDataReceived += ( s , e ) =>
@@ -103,73 +104,78 @@ public void Run()
103
104
{
104
105
if ( ! Process . StartInfo . Arguments . StartsWith ( "credential-" ) )
105
106
Logger . Trace ( $ "Running '{ Process . StartInfo . FileName . ToNPath ( ) . FileName } { Process . StartInfo . Arguments } '") ;
107
+
106
108
Process . Start ( ) ;
107
- }
108
- catch ( Win32Exception ex )
109
- {
110
- StringBuilder sb = new StringBuilder ( ) ;
111
- sb . AppendLine ( "Error code " + ex . NativeErrorCode ) ;
112
- if ( ex . NativeErrorCode == 2 )
113
- {
114
- sb . AppendLine ( "The system cannot find the file specified." ) ;
115
- }
116
- foreach ( string env in Process . StartInfo . EnvironmentVariables . Keys )
117
- {
118
- sb . AppendFormat ( "{0}:{1}" , env , Process . StartInfo . EnvironmentVariables [ env ] ) ;
119
- sb . AppendLine ( ) ;
120
- }
121
- onError ? . Invoke ( ex , String . Format ( "{0} {1}" , ex . Message , sb . ToString ( ) ) ) ;
122
- onEnd ? . Invoke ( ) ;
123
- return ;
124
- }
125
109
126
- if ( Process . StartInfo . RedirectStandardInput )
127
- Input = new StreamWriter ( Process . StandardInput . BaseStream , new UTF8Encoding ( false ) ) ;
128
- if ( Process . StartInfo . RedirectStandardError )
129
- Process . BeginErrorReadLine ( ) ;
110
+ if ( Process . StartInfo . RedirectStandardInput )
111
+ Input = new StreamWriter ( Process . StandardInput . BaseStream , new UTF8Encoding ( false ) ) ;
112
+ if ( Process . StartInfo . RedirectStandardError )
113
+ Process . BeginErrorReadLine ( ) ;
130
114
131
- onStart ? . Invoke ( ) ;
115
+ onStart ? . Invoke ( ) ;
132
116
133
- if ( Process . StartInfo . RedirectStandardOutput )
134
- {
135
- var outputStream = Process . StandardOutput ;
136
- var line = outputStream . ReadLine ( ) ;
137
- while ( line != null )
117
+ if ( Process . StartInfo . RedirectStandardOutput )
138
118
{
139
- outputProcessor . LineReceived ( line ) ;
119
+ var outputStream = Process . StandardOutput ;
120
+ var line = outputStream . ReadLine ( ) ;
121
+ while ( line != null )
122
+ {
123
+ outputProcessor . LineReceived ( line ) ;
124
+
125
+ if ( token . IsCancellationRequested )
126
+ {
127
+ if ( ! Process . HasExited )
128
+ Process . Kill ( ) ;
129
+ Process . Close ( ) ;
130
+ token . ThrowIfCancellationRequested ( ) ;
131
+ }
132
+
133
+ line = outputStream . ReadLine ( ) ;
134
+ }
135
+ outputProcessor . LineReceived ( null ) ;
136
+ }
140
137
141
- if ( token . IsCancellationRequested )
138
+ if ( Process . StartInfo . CreateNoWindow )
139
+ {
140
+ while ( ! WaitForExit ( 500 ) )
142
141
{
143
- if ( ! Process . HasExited )
142
+ if ( token . IsCancellationRequested )
143
+ {
144
144
Process . Kill ( ) ;
145
-
146
- Process . Close ( ) ;
147
- onEnd ? . Invoke ( ) ;
145
+ Process . Close ( ) ;
146
+ }
148
147
token . ThrowIfCancellationRequested ( ) ;
149
148
}
150
149
151
- line = outputStream . ReadLine ( ) ;
150
+ if ( Process . ExitCode != 0 && errors . Count > 0 )
151
+ {
152
+ thrownException = new ProcessException ( Process . ExitCode , string . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
153
+ }
152
154
}
153
- outputProcessor . LineReceived ( null ) ;
154
155
}
155
-
156
- if ( Process . StartInfo . CreateNoWindow )
156
+ catch ( Win32Exception ex )
157
157
{
158
- while ( ! WaitForExit ( 500 ) )
159
- {
160
- if ( token . IsCancellationRequested )
161
- Process . Kill ( ) ;
162
- Process . Close ( ) ;
163
- onEnd ? . Invoke ( ) ;
164
- token . ThrowIfCancellationRequested ( ) ;
165
- }
158
+ var errorCode = ex . NativeErrorCode ;
166
159
167
- if ( Process . ExitCode != 0 && errors . Count > 0 )
160
+ StringBuilder sb = new StringBuilder ( ) ;
161
+ if ( errorCode == 2 )
162
+ sb . AppendLine ( "The system cannot find the file specified." ) ;
163
+ foreach ( string env in Process . StartInfo . EnvironmentVariables . Keys )
168
164
{
169
- onError ? . Invoke ( null , string . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
165
+ sb . AppendFormat ( "{0}:{1}" , env , Process . StartInfo . EnvironmentVariables [ env ] ) ;
166
+ sb . AppendLine ( ) ;
170
167
}
168
+ thrownException = new ProcessException ( errorCode , sb . ToString ( ) , ex ) ;
169
+ }
170
+ catch ( Exception ex )
171
+ {
172
+ thrownException = new ProcessException ( Process . HasExited ? Process . ExitCode : - 42 ,
173
+ "Unknown error" ,
174
+ ex ) ;
171
175
}
172
176
177
+ if ( thrownException != null || errors . Count > 0 )
178
+ onError ? . Invoke ( thrownException , string . Join ( Environment . NewLine , errors . ToArray ( ) ) ) ;
173
179
onEnd ? . Invoke ( ) ;
174
180
}
175
181
@@ -275,11 +281,6 @@ protected virtual void ConfigureOutputProcessor()
275
281
{
276
282
}
277
283
278
- protected override void Run ( bool success )
279
- {
280
- throw new NotImplementedException ( ) ;
281
- }
282
-
283
284
protected override T RunWithReturn ( bool success )
284
285
{
285
286
var result = base . RunWithReturn ( success ) ;
@@ -293,20 +294,24 @@ protected override T RunWithReturn(bool success)
293
294
if ( outputProcessor != null )
294
295
result = outputProcessor . Result ;
295
296
296
- if ( typeof ( T ) == typeof ( string ) && result == null && ! Process . StartInfo . CreateNoWindow )
297
+ if ( typeof ( T ) == typeof ( string ) && result == null && ! Process . StartInfo . CreateNoWindow )
297
298
result = ( T ) ( object ) "Process running" ;
298
299
299
- if ( Errors != null )
300
- {
300
+ if ( ! String . IsNullOrEmpty ( Errors ) )
301
301
OnErrorData ? . Invoke ( Errors ) ;
302
- thrownException = thrownException ?? new ProcessException ( this ) ;
303
- throw thrownException ;
304
- }
305
302
}
306
303
catch ( Exception ex )
307
304
{
308
- if ( ! RaiseFaultHandlers ( ex ) )
309
- throw ;
305
+ if ( thrownException == null )
306
+ thrownException = new ProcessException ( ex . Message , ex ) ;
307
+ else
308
+ thrownException = new ProcessException ( thrownException . GetExceptionMessage ( ) , ex ) ;
309
+ }
310
+
311
+ try
312
+ {
313
+ if ( thrownException != null && ! RaiseFaultHandlers ( thrownException ) )
314
+ throw thrownException ;
310
315
}
311
316
finally
312
317
{
@@ -427,17 +432,21 @@ protected override List<T> RunWithReturn(bool success)
427
432
if ( result == null )
428
433
result = new List < T > ( ) ;
429
434
430
- if ( Errors != null )
431
- {
435
+ if ( ! String . IsNullOrEmpty ( Errors ) )
432
436
OnErrorData ? . Invoke ( Errors ) ;
433
- thrownException = thrownException ?? new ProcessException ( this ) ;
434
- throw thrownException ;
435
- }
436
437
}
437
438
catch ( Exception ex )
438
439
{
439
- if ( ! RaiseFaultHandlers ( ex ) )
440
- throw ;
440
+ if ( thrownException == null )
441
+ thrownException = new ProcessException ( ex . Message , ex ) ;
442
+ else
443
+ thrownException = new ProcessException ( thrownException . GetExceptionMessage ( ) , ex ) ;
444
+ }
445
+
446
+ try
447
+ {
448
+ if ( thrownException != null && ! RaiseFaultHandlers ( thrownException ) )
449
+ throw thrownException ;
441
450
}
442
451
finally
443
452
{
0 commit comments