Skip to content

Commit 9fdc0c2

Browse files
authored
Merge pull request #835 from adamralph/exception-constructors
Standard exception constructor equivalents
2 parents f409df1 + 18a4e32 commit 9fdc0c2

3 files changed

Lines changed: 123 additions & 11 deletions

File tree

SimpleExec/ExitCodeException.cs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,61 @@ namespace SimpleExec;
33
/// <summary>
44
/// The command exited with an unexpected exit code.
55
/// </summary>
6-
/// <param name="exitCode">The exit code of the command.</param>
76
#pragma warning disable CA1032 // Implement standard exception constructors
8-
public class ExitCodeException(int exitCode) : Exception
7+
public class ExitCodeException : Exception
98
#pragma warning restore CA1032
109
{
10+
/// <summary>
11+
/// Constructs an instance of a <see cref="ExitCodeException" />.
12+
/// </summary>
13+
/// <param name="exitCode">The exit code of the command.</param>
14+
public ExitCodeException(int exitCode) :
15+
base(CreateMessage(exitCode)) =>
16+
ExitCode = exitCode;
17+
18+
/// <summary>
19+
/// Constructs an instance of a <see cref="ExitCodeException" />.
20+
/// </summary>
21+
/// <param name="exitCode">The exit code of the command.</param>
22+
/// <param name="innerException">
23+
/// The exception that is the cause of the current exception,
24+
/// or a <c>null</c> reference (<c>Nothing</c> in Visual Basic) if no inner exception is specified.
25+
/// </param>
26+
public ExitCodeException(int exitCode, Exception innerException) :
27+
base(CreateMessage(exitCode), innerException) =>
28+
ExitCode = exitCode;
29+
30+
/// <summary>
31+
/// Constructs an instance of a <see cref="ExitCodeException" />.
32+
/// </summary>
33+
/// <param name="exitCode">The exit code of the command.</param>
34+
/// <param name="message">The message that describes the error.</param>
35+
public ExitCodeException(int exitCode, string message) :
36+
base(message) =>
37+
ExitCode = exitCode;
38+
39+
/// <summary>
40+
/// Constructs an instance of a <see cref="ExitCodeException" />.
41+
/// </summary>
42+
/// <param name="exitCode">The exit code of the command.</param>
43+
/// <param name="message">The message that describes the error.</param>
44+
/// <param name="innerException">
45+
/// The exception that is the cause of the current exception,
46+
/// or a <c>null</c> reference (<c>Nothing</c> in Visual Basic) if no inner exception is specified.
47+
/// </param>
48+
public ExitCodeException(int exitCode, string message, Exception innerException) :
49+
base(message, innerException) =>
50+
ExitCode = exitCode;
51+
1152
/// <summary>
1253
/// Gets the exit code of the command.
1354
/// </summary>
14-
public int ExitCode { get; } = exitCode;
55+
public int ExitCode { get; }
1556

16-
/// <inheritdoc/>
17-
public override string Message => $"The command exited with code {ExitCode}.";
57+
/// <summary>
58+
/// Create the message that describes the error.
59+
/// </summary>
60+
/// <param name="exitCode">The exit code of the command.</param>
61+
/// <returns>The message that describes the error</returns>
62+
protected static string CreateMessage(int exitCode) => $"The command exited with code {exitCode}.";
1863
}

SimpleExec/ExitCodeReadException.cs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,62 @@ public class ExitCodeReadException : ExitCodeException
1515
/// <param name="exitCode">The exit code of the command.</param>
1616
/// <param name="standardOutput">The contents of standard output (stdout).</param>
1717
/// <param name="standardError">The contents of standard error (stderr).</param>
18-
public ExitCodeReadException(int exitCode, string standardOutput, string standardError) : base(exitCode) => (StandardOutput, StandardError) = (standardOutput, standardError);
18+
public ExitCodeReadException(int exitCode, string standardOutput, string standardError) :
19+
base(exitCode, CreateMessage(exitCode, standardOutput, standardError))
20+
{
21+
StandardOutput = standardOutput;
22+
StandardError = standardError;
23+
}
24+
25+
/// <summary>
26+
/// Constructs an instance of a <see cref="ExitCodeReadException"/>.
27+
/// </summary>
28+
/// <param name="exitCode">The exit code of the command.</param>
29+
/// <param name="standardOutput">The contents of standard output (stdout).</param>
30+
/// <param name="standardError">The contents of standard error (stderr).</param>
31+
/// <param name="innerException">
32+
/// The exception that is the cause of the current exception,
33+
/// or a <c>null</c> reference (<c>Nothing</c> in Visual Basic) if no inner exception is specified.
34+
/// </param>
35+
public ExitCodeReadException(int exitCode, string standardOutput, string standardError, Exception innerException) :
36+
base(exitCode, CreateMessage(exitCode, standardOutput, standardError), innerException)
37+
{
38+
StandardOutput = standardOutput;
39+
StandardError = standardError;
40+
}
41+
42+
/// <summary>
43+
/// Constructs an instance of a <see cref="ExitCodeReadException"/>.
44+
/// </summary>
45+
/// <param name="exitCode">The exit code of the command.</param>
46+
/// <param name="standardOutput">The contents of standard output (stdout).</param>
47+
/// <param name="standardError">The contents of standard error (stderr).</param>
48+
/// <param name="message">The message that describes the error.</param>
49+
public ExitCodeReadException(int exitCode, string standardOutput, string standardError, string message) :
50+
base(exitCode, message)
51+
{
52+
StandardOutput = standardOutput;
53+
StandardError = standardError;
54+
}
55+
56+
/// <summary>
57+
/// Constructs an instance of a <see cref="ExitCodeReadException"/>.
58+
/// </summary>
59+
/// <param name="exitCode">The exit code of the command.</param>
60+
/// <param name="standardOutput">The contents of standard output (stdout).</param>
61+
/// <param name="standardError">The contents of standard error (stderr).</param>
62+
/// <param name="message">The message that describes the error.</param>
63+
/// <param name="innerException">
64+
/// The exception that is the cause of the current exception,
65+
/// or a <c>null</c> reference (<c>Nothing</c> in Visual Basic) if no inner exception is specified.
66+
/// </param>
67+
public ExitCodeReadException(
68+
int exitCode, string standardOutput, string standardError, string message, Exception innerException) :
69+
base(exitCode, message, innerException)
70+
{
71+
StandardOutput = standardOutput;
72+
StandardError = standardError;
73+
}
1974

2075
/// <summary>
2176
/// Gets the contents of standard output (stdout).
@@ -27,7 +82,13 @@ public class ExitCodeReadException : ExitCodeException
2782
/// </summary>
2883
public string StandardError { get; }
2984

30-
/// <inheritdoc/>
31-
public override string Message =>
32-
$"{base.Message}{TwoNewLines}Standard output (stdout):{TwoNewLines}{StandardOutput}{TwoNewLines}Standard error (stderr):{TwoNewLines}{StandardError}";
85+
/// <summary>
86+
/// Create the message that describes the error.
87+
/// </summary>
88+
/// <param name="exitCode">The exit code of the command.</param>
89+
/// <param name="standardOutput">The contents of standard output (stdout).</param>
90+
/// <param name="standardError">The contents of standard error (stderr).</param>
91+
/// <returns>The message that describes the error</returns>
92+
protected static string CreateMessage(int exitCode, string standardOutput, string standardError) =>
93+
$"{CreateMessage(exitCode)}{TwoNewLines}Standard output (stdout):{TwoNewLines}{standardOutput}{TwoNewLines}Standard error (stderr):{TwoNewLines}{standardError}";
3394
}

SimpleExecTests/PublicApi.IsVerified.verified.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@
1818
public class ExitCodeException : System.Exception
1919
{
2020
public ExitCodeException(int exitCode) { }
21+
public ExitCodeException(int exitCode, System.Exception innerException) { }
22+
public ExitCodeException(int exitCode, string message) { }
23+
public ExitCodeException(int exitCode, string message, System.Exception innerException) { }
2124
public int ExitCode { get; }
22-
public override string Message { get; }
25+
protected static string CreateMessage(int exitCode) { }
2326
}
2427
public class ExitCodeReadException : SimpleExec.ExitCodeException
2528
{
2629
public ExitCodeReadException(int exitCode, string standardOutput, string standardError) { }
27-
public override string Message { get; }
30+
public ExitCodeReadException(int exitCode, string standardOutput, string standardError, System.Exception innerException) { }
31+
public ExitCodeReadException(int exitCode, string standardOutput, string standardError, string message) { }
32+
public ExitCodeReadException(int exitCode, string standardOutput, string standardError, string message, System.Exception innerException) { }
2833
public string StandardError { get; }
2934
public string StandardOutput { get; }
35+
protected static string CreateMessage(int exitCode, string standardOutput, string standardError) { }
3036
}
3137
}

0 commit comments

Comments
 (0)