8000 Improve process helper by asbjornu · Pull Request #38 · GitTools/GitTools.Core · GitHub
[go: up one dir, main page]

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

Improve process helper #38

Merged
merged 6 commits into from
Nov 16, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added NativeErrorCode enum, cleaned up catch logic and throw Director…
…yNotFoundException on error code 3.
  • Loading branch information
asbjornu committed Nov 1, 2016
commit 87ea2ede1090167821c4895fb056084ab9dc2408
56 changes: 40 additions & 16 deletions src/GitTools.Core/GitTools.Core.Shared/Helpers/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ public static Process Start(ProcessStartInfo startInfo)
}
catch (Win32Exception exception)
{
// NOTE: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
// -- @asbjornu
if (exception.NativeErrorCode == 2)
switch ((NativeErrorCode)exception.NativeErrorCode)
{
throw new FileNotFoundException(string.Format("The executable file '{0}' could not be found.",
startInfo.FileName),
startInfo.FileName,
exception);
case NativeErrorCode.Success:
// Success is not a failure.
break;

case NativeErrorCode.FileNotFound:
throw new FileNotFoundException(string.Format("The executable file '{0}' could not be found.",
startInfo.FileName),
startInfo.FileName,
exception);

case NativeErrorCode.PathNotFound:
throw new DirectoryNotFoundException(string.Format("The path to the executable file '{0}' could not be found.",
startInfo.FileName),
exception);
}

throw;
Expand All @@ -47,19 +55,24 @@ public static Process Start(ProcessStartInfo startInfo)
process.PriorityClass = ProcessPriorityClass.Idle;
}
}
catch (Win32Exception exception)
catch
{

// NOTE: It seems like in some situations, setting the priority class will throw an exception
// NOTE: It seems like in some situations, setting the priority class will throw a Win32Exception
// with the error code set to "Success", which I think we can safely interpret as a success and
// not an exception.
// See https://travis-ci.org/GitTools/GitVersion/jobs/171288284#L2026
// and https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
//
// See: https://travis-ci.org/GitTools/GitVersion/jobs/171288284#L2026
// And: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
//
// There's also the case where the process might be killed before we try to adjust its priority
// class, in which 8000 case it will throw an InvalidOperationException. What we ideally should do
// is start the process in a "suspended" state, adjust the priority class, then resume it, but
// that's not possible in pure .NET.
//
// See: https://travis-ci.org/GitTools/GitVersion/jobs/166709203#L2278
// And: http://www.codeproject.com/Articles/230005/Launch-a-process-suspended
//
// -- @asbjornu
if (exception.NativeErrorCode == 0)
{
throw;
}
}
}
}
Expand Down Expand Up @@ -142,6 +155,17 @@ public static int Run(Action<string> output, Action<string> errorOutput, TextRea
}
}

/// <summary>
/// System error codes.
/// See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx
/// </summary>
private enum NativeErrorCode
{
Success = 0x0,
FileNotFound = 0x2,
PathNotFound = 0x3
}

[Flags]
public enum ErrorModes
{
Expand Down
0