Enhance error reporting when running wixnative.exe
Rob Mensching committed
Dec 2, 2021 at 13:47 UTC
6bbe61062a3f3b22ddfe3501f9be688a74559694
2 files changed
+45
-4
src/wix/WixToolset.Core.Native/WixNativeException.cs
new
+38
@@ -0,0 +1,38 @@
1
+// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
+
3
+namespace WixToolset.Core.Native
4
+{
5
+ using System;
6
+ using System.Collections.Generic;
7
+ using System.ComponentModel;
8
+ using System.Runtime.Serialization;
9
+
10
+ [Serializable]
11
+ internal class WixNativeException : Exception
12
+ {
13
+ private static readonly string LineSeparator = Environment.NewLine + " ";
14
+
15
+ public WixNativeException()
16
+ {
17
+ }
18
+
19
+ public WixNativeException(string message) : base(message)
20
+ {
21
+ }
22
+
23
+ public WixNativeException(string message, Exception innerException) : base(message, innerException)
24
+ {
25
+ }
26
+
27
+ protected WixNativeException(SerializationInfo info, StreamingContext context) : base(info, context)
28
+ {
29
+ }
30
+
31
+ public static WixNativeException FromOutputLines(int errorCode, IReadOnlyCollection<string> lines)
32
+ {
33
+ var exception = new Win32Exception(errorCode);
34
+ var output = String.Join(LineSeparator, lines);
35
+ return new WixNativeException($"wixnative.exe failed with error code: {exception.ErrorCode} - {exception.Message} Output:{LineSeparator}{output}", exception);
36
+ }
37
+ }
38
+}
src/wix/WixToolset.Core.Native/WixNativeExe.cs
+7
-4
@@ -39,17 +39,20 @@ namespace WixToolset.Core.Native
39
{
40
RedirectStandardInput = true,
41
RedirectStandardOutput = true,
42
+ RedirectStandardError = true,
43
CreateNoWindow = true,
44
ErrorDialog = false,
45
UseShellExecute = false
46
};
47
47
- var stdoutLines = new List<string>();
48
+ var outputLines = new List<string>();
49
50
using (var process = Process.Start(wixNativeInfo))
51
{
51
- process.OutputDataReceived += (s, a) => stdoutLines.Add(a.Data);
52
+ process.OutputDataReceived += (s, a) => outputLines.Add(a.Data);
53
+ process.ErrorDataReceived += (s, a) => outputLines.Add(a.Data);
54
process.BeginOutputReadLine();
55
+ process.BeginErrorReadLine();
56
57
if (this.stdinLines.Count > 0)
58
{
@@ -69,11 +72,11 @@ namespace WixToolset.Core.Native
72
73
if (process.ExitCode != 0)
74
{
72
- throw new Win32Exception(process.ExitCode);
75
+ throw WixNativeException.FromOutputLines(process.ExitCode, outputLines);
76
}
77
}
78
76
- return stdoutLines;
79
+ return outputLines;
80
}
81
82
private static void EnsurePathToWixNativeExeSet()