aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.WindowsInstaller/Msi/MsiException.cs
blob: b33bf27a60afcaecca5f95cb72a319095df85356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 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;

    /// <summary>
    /// Exception that wraps MsiGetLastError().
    /// </summary>
    [Serializable]
    public class MsiException : Win32Exception
    {
        /// <summary>
        /// Instantiate a new MsiException with a given error.
        /// </summary>
        /// <param name="error">The error code from the MsiXxx() function call.</param>
        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;
        }

        /// <summary>
        /// Gets the error number.
        /// </summary>
        public int Error { get; private set; }

        /// <summary>
        /// Gets the internal MSI error number.
        /// </summary>
        public int MsiError { get; private set; }

        /// <summary>
        /// Gets any additional the error information.
        /// </summary>
        public string[] ErrorInfo { get; private set; }

        /// <summary>
        /// Overrides Message property to return useful error message.
        /// </summary>
        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));
                }
            }
        }
    }
}