| 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.Msi |
| 4 | { |
| 5 | using System; |
| 6 | using System.ComponentModel; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Exception that wraps MsiGetLastError(). |
| 10 | /// </summary> |
| 11 | [Serializable] |
| 12 | public sealed class MsiException : Win32Exception |
| 13 | { |
| 14 | /// <summary> |
| 15 | /// Instantiate a new MsiException with a given error. |
| 16 | /// </summary> |
| 17 | /// <param name="error">The error code from the MsiXxx() function call.</param> |
| 18 | public MsiException(int error) : base(error) |
| 19 | { |
| 20 | IntPtr handle = MsiInterop.MsiGetLastErrorRecord(); |
| 21 | if (IntPtr.Zero != handle) |
| 22 | { |
| 23 | using (Record record = new Record(handle)) |
| 24 | { |
| 25 | this.MsiError = record.GetInteger(1); |
| 26 | |
| 27 | int errorInfoCount = record.GetFieldCount() - 1; |
| 28 | this.ErrorInfo = new string[errorInfoCount]; |
| 29 | for (int i = 0; i < errorInfoCount; ++i) |
| 30 | { |
| 31 | this.ErrorInfo[i] = record.GetString(i + 2); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | else |
| 36 | { |
| 37 | this.MsiError = 0; |
| 38 | this.ErrorInfo = new string[0]; |
| 39 | } |
| 40 | |
| 41 | this.Error = error; |
| 42 | } |
| 43 | |
| 44 | /// <summary> |
| 45 | /// Gets the error number. |
| 46 | /// </summary> |
| 47 | public int Error { get; private set; } |
| 48 | |
| 49 | /// <summary> |
| 50 | /// Gets the internal MSI error number. |
| 51 | /// </summary> |
| 52 | public int MsiError { get; private set; } |
| 53 | |
| 54 | /// <summary> |
| 55 | /// Gets any additional the error information. |
| 56 | /// </summary> |
| 57 | public string[] ErrorInfo { get; private set; } |
| 58 | |
| 59 | /// <summary> |
| 60 | /// Overrides Message property to return useful error message. |
| 61 | /// </summary> |
| 62 | public override string Message |
| 63 | { |
| 64 | get |
| 65 | { |
| 66 | if (0 == this.MsiError) |
| 67 | { |
| 68 | return base.Message; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | return String.Format("Internal MSI failure. Win32 error: {0}, MSI error: {1}, detail: {2}", this.Error, this.MsiError, String.Join(", ", this.ErrorInfo)); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |