Browse Source

Throw helpers for ProcessException

pull/438/head^2
ionite34 10 months ago
parent
commit
a91bbb9319
No known key found for this signature in database
GPG Key ID: B3404C5F3827849B
  1. 61
      StabilityMatrix.Core/Exceptions/ProcessException.cs

61
StabilityMatrix.Core/Exceptions/ProcessException.cs

@ -1,4 +1,6 @@
using StabilityMatrix.Core.Processes;
using System.Diagnostics;
using System.Text;
using StabilityMatrix.Core.Processes;
namespace StabilityMatrix.Core.Exceptions;
@ -14,9 +16,64 @@ public class ProcessException : Exception
public ProcessException(ProcessResult processResult)
: base(
$"Process {processResult.ProcessName} exited with code {processResult.ExitCode}. {{StdOut = {processResult.StandardOutput}, StdErr = {processResult.StandardError}}}"
$"Process {processResult.ProcessName} exited with code {processResult.ExitCode}. "
+ $"{{StdOut = {processResult.StandardOutput}, StdErr = {processResult.StandardError}}}"
)
{
ProcessResult = processResult;
}
public static void ThrowIfNonZeroExitCode(ProcessResult processResult)
{
if (processResult.IsSuccessExitCode)
return;
throw new ProcessException(processResult);
}
public static void ThrowIfNonZeroExitCode(Process process, string output)
{
if (!process.HasExited || process.ExitCode == 0)
return;
throw new ProcessException(
new ProcessResult
{
ProcessName = process.StartInfo.FileName,
ExitCode = process.ExitCode,
StandardOutput = output
}
);
}
public static void ThrowIfNonZeroExitCode(Process process, StringBuilder outputBuilder)
{
if (!process.HasExited || process.ExitCode == 0)
return;
throw new ProcessException(
new ProcessResult
{
ProcessName = process.StartInfo.FileName,
ExitCode = process.ExitCode,
StandardOutput = outputBuilder.ToString()
}
);
}
public static void ThrowIfNonZeroExitCode(Process process, string stdOut, string stdErr)
{
if (!process.HasExited || process.ExitCode == 0)
return;
throw new ProcessException(
new ProcessResult
{
ProcessName = process.StartInfo.FileName,
ExitCode = process.ExitCode,
StandardOutput = stdOut,
StandardError = stdErr
}
);
}
}

Loading…
Cancel
Save