From 6bbe61062a3f3b22ddfe3501f9be688a74559694 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Thu, 2 Dec 2021 13:47:10 -0800 Subject: Enhance error reporting when running wixnative.exe --- .../WixToolset.Core.Native/WixNativeException.cs | 38 ++++++++++++++++++++++ src/wix/WixToolset.Core.Native/WixNativeExe.cs | 11 ++++--- 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/wix/WixToolset.Core.Native/WixNativeException.cs diff --git a/src/wix/WixToolset.Core.Native/WixNativeException.cs b/src/wix/WixToolset.Core.Native/WixNativeException.cs new file mode 100644 index 00000000..950e973e --- /dev/null +++ b/src/wix/WixToolset.Core.Native/WixNativeException.cs @@ -0,0 +1,38 @@ +// 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. + +namespace WixToolset.Core.Native +{ + using System; + using System.Collections.Generic; + using System.ComponentModel; + using System.Runtime.Serialization; + + [Serializable] + internal class WixNativeException : Exception + { + private static readonly string LineSeparator = Environment.NewLine + " "; + + public WixNativeException() + { + } + + public WixNativeException(string message) : base(message) + { + } + + public WixNativeException(string message, Exception innerException) : base(message, innerException) + { + } + + protected WixNativeException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + + public static WixNativeException FromOutputLines(int errorCode, IReadOnlyCollection lines) + { + var exception = new Win32Exception(errorCode); + var output = String.Join(LineSeparator, lines); + return new WixNativeException($"wixnative.exe failed with error code: {exception.ErrorCode} - {exception.Message} Output:{LineSeparator}{output}", exception); + } + } +} diff --git a/src/wix/WixToolset.Core.Native/WixNativeExe.cs b/src/wix/WixToolset.Core.Native/WixNativeExe.cs index fb41b2f2..6e8d64ca 100644 --- a/src/wix/WixToolset.Core.Native/WixNativeExe.cs +++ b/src/wix/WixToolset.Core.Native/WixNativeExe.cs @@ -39,17 +39,20 @@ namespace WixToolset.Core.Native { RedirectStandardInput = true, RedirectStandardOutput = true, + RedirectStandardError = true, CreateNoWindow = true, ErrorDialog = false, UseShellExecute = false }; - var stdoutLines = new List(); + var outputLines = new List(); using (var process = Process.Start(wixNativeInfo)) { - process.OutputDataReceived += (s, a) => stdoutLines.Add(a.Data); + process.OutputDataReceived += (s, a) => outputLines.Add(a.Data); + process.ErrorDataReceived += (s, a) => outputLines.Add(a.Data); process.BeginOutputReadLine(); + process.BeginErrorReadLine(); if (this.stdinLines.Count > 0) { @@ -69,11 +72,11 @@ namespace WixToolset.Core.Native if (process.ExitCode != 0) { - throw new Win32Exception(process.ExitCode); + throw WixNativeException.FromOutputLines(process.ExitCode, outputLines); } } - return stdoutLines; + return outputLines; } private static void EnsurePathToWixNativeExeSet() -- cgit v1.2.3-55-g6feb