Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 87ea2ed

Browse files
committed
Added NativeErrorCode enum, cleaned up catch logic and throw DirectoryNotFoundException on error code 3.
1 parent c1f332d commit 87ea2ed

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

src/GitTools.Core/GitTools.Core.Shared/Helpers/ProcessHelper.cs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,22 @@ public static Process Start(ProcessStartInfo startInfo)
2727
}
2828
catch (Win32Exception exception)
2929
{
30-
// NOTE: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
31-
// -- @asbjornu
32-
if (exception.NativeErrorCode == 2)
30+
switch ((NativeErrorCode)exception.NativeErrorCode)
3331
{
34-
throw new FileNotFoundException(string.Format("The executable file '{0}' could not be found.",
35-
startInfo.FileName),
36-
startInfo.FileName,
37-
exception);
32+
case NativeErrorCode.Success:
33+
// Success is not a failure.
34+
break;
35+
36+
case NativeErrorCode.FileNotFound:
37+
throw new FileNotFoundException(string.Format("The executable file '{0}' could not be found.",
38+
startInfo.FileName),
39+
startInfo.FileName,
40+
exception);
41+
42+
case NativeErrorCode.PathNotFound:
43+
throw new DirectoryNotFoundException(string.Format("The path to the executable file '{0}' could not be found.",
44+
startInfo.FileName),
45+
exception);
3846
}
3947

4048
throw;
@@ -47,19 +55,24 @@ public static Process Start(ProcessStartInfo startInfo)
4755
process.PriorityClass = ProcessPriorityClass.Idle;
4856
}
4957
}
50-
catch (Win32Exception exception)
58+
catch
5159
{
52-
53-
// NOTE: It seems like in some situations, setting the priority class will throw an exception
60+
// NOTE: It seems like in some situations, setting the priority class will throw a Win32Exception
5461
// with the error code set to "Success", which I think we can safely interpret as a success and
5562
// not an exception.
56-
// See https://travis-ci.org/GitTools/GitVersion/jobs/171288284#L2026
57-
// and https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
63+
//
64+
// See: https://travis-ci.org/GitTools/GitVersion/jobs/171288284#L2026
65+
// And: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
66+
//
67+
// There's also the case where the process might be killed before we try to adjust its priority
68+
// class, in which case it will throw an InvalidOperationException. What we ideally should do
69+
// is start the process in a "suspended" state, adjust the priority class, then resume it, but
70+
// that's not possible in pure .NET.
71+
//
72+
// See: https://travis-ci.org/GitTools/GitVersion/jobs/166709203#L2278
73+
// And: http://www.codeproject.com/Articles/230005/Launch-a-process-suspended
74+
//
5875
// -- @asbjornu
59-
if (exception.NativeErrorCode == 0)
60-
{
61-
throw;
62-
}
6376
}
6477
}
6578
}
@@ -142,6 +155,17 @@ public static int Run(Action<string> output, Action<string> errorOutput, TextRea
142155
}
143156
}
144157

158+
/// <summary>
159+
/// System error codes.
160+
/// See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
161+
/// </summary>
162+
private enum NativeErrorCode
163+
{
164+
Success = 0x0,
165+
FileNotFound = 0x2,
166+
PathNotFound = 0x3
167+
}
168+
145169
[Flags]
146170
public enum ErrorModes
147171
{

0 commit comments

Comments
 (0)