src/jdk.incubator.jpackage/windows/native/libjpackage/WinErrorHandling.cpp
author herrick
Fri, 08 Nov 2019 14:53:03 -0500
branchJDK-8200758-branch
changeset 58994 b09ba68c6a19
parent 57909 src/jdk.jpackage/windows/native/libjpackage/WinErrorHandling.cpp@c7de06ed4b54
permissions -rw-r--r--
8233636 : Make jpackage an incubator and remove tool provider implementation Reviewed-by: asemenyuk, almatvee, kcr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57413
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     1
/*
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     4
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    10
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    15
 * accompanied this code).
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    16
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    20
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    23
 * questions.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    24
 */
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    25
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    26
#include "WinErrorHandling.h"
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    27
#include "Log.h"
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    28
#include "SysInfo.h"
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    29
#include "FileUtils.h"
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    30
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    31
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    32
namespace {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    33
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    34
std::string makeMessage(const std::string& msg, const char* label,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    35
                                            const void* c, DWORD errorCode) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    36
    std::ostringstream err;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    37
    err << (label ? label : "Some error") << " [" << errorCode << "]";
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    38
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    39
    HMODULE hmodule = NULL;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    40
    if (c) {
57909
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    41
        GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    42
                | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    43
                reinterpret_cast<LPCTSTR>(c), &hmodule);
57413
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    44
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    45
        if (!hmodule) {
57909
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    46
            LOG_WARNING(tstrings::any() << "GetModuleHandleEx() failed for "
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    47
                    << c << " address.");
57413
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    48
        }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    49
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    50
    if (hmodule || !c) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    51
        err << "(" << SysError::getSysErrorMessage(errorCode, hmodule) << ")";
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    52
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    53
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    54
    return joinErrorMessages(msg, err.str());
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    55
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    56
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    57
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    58
std::wstring getSystemMessageDescription(DWORD messageId, HMODULE moduleHandle) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    59
    LPWSTR pMsg = NULL;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    60
    std::wstring descr;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    61
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    62
    // we always retrieve UNICODE description from system,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    63
    // convert it to utf8 if UNICODE is not defined
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    64
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    65
    while (true) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    66
        DWORD res = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER
57909
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    67
                | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    68
                | (moduleHandle != NULL ? FORMAT_MESSAGE_FROM_HMODULE : 0),
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    69
                moduleHandle, messageId, 0, (LPWSTR)&pMsg, 0, NULL);
57413
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    70
        if (res > 0) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    71
            // replace all non-printed chars with space
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    72
            for (DWORD i=0; i<res; i++) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    73
                if (pMsg[i] < L' ') {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    74
                    pMsg[i] = L' ';
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    75
                }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    76
            }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    77
            // trim right (spaces and dots)
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    78
            for (DWORD i=res; i>0; i--) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    79
                if (pMsg[i] > L' ' && pMsg[i] != L'.') {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    80
                    break;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    81
                }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    82
                pMsg[i] = 0;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    83
            }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    84
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    85
            descr = pMsg;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    86
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    87
            LocalFree(pMsg);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    88
        } else {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    89
            // if we fail to get description for specific moduleHandle,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    90
            // try to get "common" description.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    91
            if (moduleHandle != NULL) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    92
                moduleHandle = NULL;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    93
                continue;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    94
            }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    95
            descr = L"No description available";
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    96
        }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    97
        break;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    98
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    99
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   100
    return descr;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   101
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   102
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   103
} // namespace
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   104
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   105
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   106
SysError::SysError(const tstrings::any& msg, const void* caller, DWORD ec,
57909
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   107
        const char* label):
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   108
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   109
std::runtime_error(makeMessage(msg.str(), label, caller, ec)) {
57413
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   110
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   111
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   112
std::wstring SysError::getSysErrorMessage(DWORD errCode, HMODULE moduleHandle) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   113
    tstrings::any msg;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   114
    msg << "system error " << errCode
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   115
        << " (" << getSystemMessageDescription(errCode, moduleHandle) << ")";
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   116
    return msg.tstr();
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   117
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   118
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   119
std::wstring SysError::getComErrorMessage(HRESULT hr) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   120
    HRESULT hrOrig = hr;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   121
    // for FACILITY_WIN32 facility we need to reset hiword
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   122
    if(HRESULT_FACILITY(hr) == FACILITY_WIN32) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   123
        hr = HRESULT_CODE(hr);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   124
    }
57909
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   125
    return tstrings::format(_T("COM error 0x%08X (%s)"), hrOrig,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   126
            getSystemMessageDescription(hr, NULL));
57413
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   127
}