// 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.Msi { using System; using System.ComponentModel; using WixToolset.Core.Native; /// /// Exception that wraps MsiGetLastError(). /// [Serializable] public class MsiException : Win32Exception { /// /// Instantiate a new MsiException with a given error. /// /// The error code from the MsiXxx() function call. public MsiException(int error) : base(error) { uint handle = MsiInterop.MsiGetLastErrorRecord(); if (0 != handle) { using (Record record = new Record(handle)) { this.MsiError = record.GetInteger(1); int errorInfoCount = record.GetFieldCount() - 1; this.ErrorInfo = new string[errorInfoCount]; for (int i = 0; i < errorInfoCount; ++i) { this.ErrorInfo[i] = record.GetString(i + 2); } } } else { this.MsiError = 0; this.ErrorInfo = new string[0]; } this.Error = error; } /// /// Gets the error number. /// public int Error { get; private set; } /// /// Gets the internal MSI error number. /// public int MsiError { get; private set; } /// /// Gets any additional the error information. /// public string[] ErrorInfo { get; private set; } /// /// Overrides Message property to return useful error message. /// public override string Message { get { if (0 == this.MsiError) { return base.Message; } else { return String.Format("Internal MSI failure. Win32 error: {0}, MSI error: {1}, detail: {2}", this.Error, this.MsiError, String.Join(", ", this.ErrorInfo)); } } } } }