Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when resource.pri is not present #1443

Merged
merged 6 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ pkindex
PMS
positionals
powertoys
pri
processthreads
productcode
pseudocode
Expand Down
6 changes: 6 additions & 0 deletions src/AppInstallerCLICore/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,12 @@ namespace AppInstaller::CLI
context.Reporter.Error() << Resource::String::DisabledByGroupPolicy << ": "_liv << policy.PolicyName() << std::endl;
return APPINSTALLER_CLI_ERROR_BLOCKED_BY_POLICY;
}
catch (const Resource::ResourceOpenException& e)
{
Logging::Telemetry().LogException(command->FullName(), "ResourceOpenException", e.what());
context.Reporter.Error() << GetUserPresentableMessage(e) << std::endl;
return APPINSTALLER_CLI_ERROR_MISSING_RESOURCE_FILE;
}
catch (const std::exception& e)
{
Logging::Telemetry().LogException(command->FullName(), "std::exception", e.what());
Expand Down
26 changes: 23 additions & 3 deletions src/AppInstallerCLICore/Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using namespace AppInstaller::Utility::literals;


namespace AppInstaller::CLI::Resource
{
LocString::LocString(StringId id) : Utility::LocIndString(Loader::Instance().ResolveString(id)) {}
Expand All @@ -16,9 +15,25 @@ namespace AppInstaller::CLI::Resource
return instance;
}

Loader::Loader()
Loader::Loader() : m_wingetLoader(nullptr)
{
m_wingetLoader = winrt::Windows::ApplicationModel::Resources::ResourceLoader::GetForViewIndependentUse(L"winget");
try
{
// The default constructor of ResourceLoader throws a winrt::hresult_error exception
// when resource.pri is not found. ResourceLoader::GetForViewIndependentUse also throws
// a winrt::hresult_error but for reasons unknown it only gets catch when running on the
// debugger. Running without a debugger will result in a crash that not even adding a
// catch all fix. To provide a good error message we call the default constructor
// before calling GetForViewIndependentUse.
m_wingetLoader = winrt::Windows::ApplicationModel::Resources::ResourceLoader();
m_wingetLoader = winrt::Windows::ApplicationModel::Resources::ResourceLoader::GetForViewIndependentUse(L"winget");
}
catch (const winrt::hresult_error& hre)
{
// This message cannot be localized.
AICLI_LOG(CLI, Error, << "Failure loading resource file with error: " << hre.code());
throw ResourceOpenException(hre);
}
}

std::string Loader::ResolveString(
Expand All @@ -36,4 +51,9 @@ namespace AppInstaller::CLI::Resource

THROW_HR(E_UNEXPECTED);
}

ResourceOpenException::ResourceOpenException(const winrt::hresult_error& hre)
{
m_message = "Could not open the resource file: " + GetUserPresentableMessage(hre);
}
}
10 changes: 10 additions & 0 deletions src/AppInstallerCLICore/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,16 @@ namespace AppInstaller::CLI::Resource
};

Utility::LocIndView GetFixedString(FixedString fs);

struct ResourceOpenException : std::exception
{
ResourceOpenException(const winrt::hresult_error& hre);

const char* what() const noexcept override { return m_message.c_str(); }

private:
std::string m_message;
};
}

inline std::ostream& operator<<(std::ostream& out, AppInstaller::CLI::Resource::StringId si)
Expand Down
4 changes: 3 additions & 1 deletion src/AppInstallerCommonCore/Errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ namespace AppInstaller
#ifndef WINGET_DISABLE_FOR_FUZZING
std::string GetUserPresentableMessage(const winrt::hresult_error& hre)
{
return Utility::ConvertToUTF8(hre.message());
std::ostringstream strstr;
GetUserPresentableMessageForHR(strstr, hre.code());
return strstr.str();
}
#endif
}
1 change: 1 addition & 0 deletions src/AppInstallerCommonCore/Public/AppInstallerErrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#define APPINSTALLER_CLI_ERROR_SOURCE_OPEN_FAILED ((HRESULT)0x8a150045)
#define APPINSTALLER_CLI_ERROR_SOURCE_AGREEMENTS_NOT_ACCEPTED ((HRESULT)0x8a150046)
#define APPINSTALLER_CLI_ERROR_CUSTOMHEADER_EXCEEDS_MAXLENGTH ((HRESULT)0x8a150047)
#define APPINSTALLER_CLI_ERROR_MISSING_RESOURCE_FILE ((HRESULT)0x8a150048)


namespace AppInstaller
Expand Down