Skip to content

Commit

Permalink
Add extended error codes to results without them (#4858)
Browse files Browse the repository at this point in the history
## Change
Adds `HRESULT ExtendedErrorCode{ get; };` to the `FindPackagesResult`
and `ConnectResult` classes that did not have them. They work just like
the other cases, exposing the raw `HRESULT` value from their respective
operations.
  • Loading branch information
JohnMcPMS authored Oct 8, 2024
1 parent 8a31bfd commit 8f6b6f8
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 105 deletions.
50 changes: 35 additions & 15 deletions src/AppInstallerCLICore/Workflows/WorkflowBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ namespace AppInstaller::CLI::Workflow
return authArgs;
}

HRESULT HandleException(Execution::Context& context, std::exception_ptr exception)
HRESULT HandleException(Execution::Context* context, std::exception_ptr exception)
{
try
{
Expand All @@ -399,47 +399,67 @@ namespace AppInstaller::CLI::Workflow
{
// Even though they are logged at their source, log again here for completeness.
Logging::Telemetry().LogException(Logging::FailureTypeEnum::ResultException, re.what());
context.Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
GetUserPresentableMessage(re) << std::endl;
if (context)
{
context->Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
GetUserPresentableMessage(re) << std::endl;
}
return re.GetErrorCode();
}
catch (const winrt::hresult_error& hre)
{
std::string message = GetUserPresentableMessage(hre);
Logging::Telemetry().LogException(Logging::FailureTypeEnum::WinrtHResultError, message);
context.Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
message << std::endl;
if (context)
{
context->Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
message << std::endl;
}
return hre.code();
}
catch (const Settings::GroupPolicyException& e)
{
auto policy = Settings::TogglePolicy::GetPolicy(e.Policy());
auto policyNameId = policy.PolicyName();
context.Reporter.Error() << Resource::String::DisabledByGroupPolicy(policyNameId) << std::endl;
if (context)
{
auto policy = Settings::TogglePolicy::GetPolicy(e.Policy());
auto policyNameId = policy.PolicyName();
context->Reporter.Error() << Resource::String::DisabledByGroupPolicy(policyNameId) << std::endl;
}
return APPINSTALLER_CLI_ERROR_BLOCKED_BY_POLICY;
}
catch (const std::exception& e)
{
Logging::Telemetry().LogException(Logging::FailureTypeEnum::StdException, e.what());
context.Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
GetUserPresentableMessage(e) << std::endl;
if (context)
{
context->Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
GetUserPresentableMessage(e) << std::endl;
}
return APPINSTALLER_CLI_ERROR_COMMAND_FAILED;
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
Logging::Telemetry().LogException(Logging::FailureTypeEnum::Unknown, {});
context.Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << " ???"_liv << std::endl;
if (context)
{
context->Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << " ???"_liv << std::endl;
}
return APPINSTALLER_CLI_ERROR_COMMAND_FAILED;
}

return E_UNEXPECTED;
}

HRESULT HandleException(Execution::Context& context, std::exception_ptr exception)
{
return HandleException(&context, exception);
}

void OpenSource::operator()(Execution::Context& context) const
{
std::string_view sourceName;
Expand Down
4 changes: 4 additions & 0 deletions src/AppInstallerCLICore/Workflows/WorkflowBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ namespace AppInstaller::CLI::Workflow
// Helper to create authentication arguments from context input.
Authentication::AuthenticationArguments GetAuthenticationArguments(const Execution::Context& context);

// Helper to report exceptions and return the HRESULT.
// If context is null, no output will be attempted.
HRESULT HandleException(Execution::Context* context, std::exception_ptr exception);

// Helper to report exceptions and return the HRESULT.
HRESULT HandleException(Execution::Context& context, std::exception_ptr exception);

Expand Down
10 changes: 8 additions & 2 deletions src/Microsoft.Management.Deployment/ConnectResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

namespace winrt::Microsoft::Management::Deployment::implementation
{
void ConnectResult::Initialize(winrt::Microsoft::Management::Deployment::ConnectResultStatus status, winrt::Microsoft::Management::Deployment::PackageCatalog packageCatalog)
void ConnectResult::Initialize(winrt::Microsoft::Management::Deployment::ConnectResultStatus status, winrt::Microsoft::Management::Deployment::PackageCatalog packageCatalog, winrt::hresult extendedErrorCode)
{
m_status = status;
m_packageCatalog = packageCatalog;
m_packageCatalog = packageCatalog;
m_extendedErrorCode = extendedErrorCode;
}
winrt::Microsoft::Management::Deployment::ConnectResultStatus ConnectResult::Status()
{
Expand All @@ -18,5 +19,10 @@ namespace winrt::Microsoft::Management::Deployment::implementation
winrt::Microsoft::Management::Deployment::PackageCatalog ConnectResult::PackageCatalog()
{
return m_packageCatalog;
}

winrt::hresult ConnectResult::ExtendedErrorCode()
{
return m_extendedErrorCode;
}
}
4 changes: 3 additions & 1 deletion src/Microsoft.Management.Deployment/ConnectResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ namespace winrt::Microsoft::Management::Deployment::implementation
ConnectResult() = default;

#if !defined(INCLUDE_ONLY_INTERFACE_METHODS)
void Initialize(winrt::Microsoft::Management::Deployment::ConnectResultStatus status, winrt::Microsoft::Management::Deployment::PackageCatalog packageCatalog);
void Initialize(winrt::Microsoft::Management::Deployment::ConnectResultStatus status, winrt::Microsoft::Management::Deployment::PackageCatalog packageCatalog, winrt::hresult extendedErrorCode);
#endif

winrt::Microsoft::Management::Deployment::ConnectResultStatus Status();
winrt::Microsoft::Management::Deployment::PackageCatalog PackageCatalog();
winrt::hresult ExtendedErrorCode();

#if !defined(INCLUDE_ONLY_INTERFACE_METHODS)
private:
winrt::Microsoft::Management::Deployment::ConnectResultStatus m_status = winrt::Microsoft::Management::Deployment::ConnectResultStatus::Ok;
winrt::Microsoft::Management::Deployment::PackageCatalog m_packageCatalog{ nullptr };
winrt::hresult m_extendedErrorCode = S_OK;
#endif
};
}
9 changes: 8 additions & 1 deletion src/Microsoft.Management.Deployment/FindPackagesResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ namespace winrt::Microsoft::Management::Deployment::implementation
void FindPackagesResult::Initialize(
winrt::Microsoft::Management::Deployment::FindPackagesResultStatus status,
bool wasLimitExceeded,
Windows::Foundation::Collections::IVector<Microsoft::Management::Deployment::MatchResult> matches)
Windows::Foundation::Collections::IVector<Microsoft::Management::Deployment::MatchResult> matches,
winrt::hresult extendedErrorCode)
{
m_status = status;
m_matches = matches;
m_wasLimitExceeded = wasLimitExceeded;
m_extendedErrorCode = extendedErrorCode;
}
winrt::Microsoft::Management::Deployment::FindPackagesResultStatus FindPackagesResult::Status()
{
Expand All @@ -28,4 +30,9 @@ namespace winrt::Microsoft::Management::Deployment::implementation
{
return m_wasLimitExceeded;
}

winrt::hresult FindPackagesResult::ExtendedErrorCode()
{
return m_extendedErrorCode;
}
}
5 changes: 4 additions & 1 deletion src/Microsoft.Management.Deployment/FindPackagesResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ namespace winrt::Microsoft::Management::Deployment::implementation
void Initialize(
winrt::Microsoft::Management::Deployment::FindPackagesResultStatus status,
bool wasLimitExceeded,
Windows::Foundation::Collections::IVector<winrt::Microsoft::Management::Deployment::MatchResult> matches);
Windows::Foundation::Collections::IVector<winrt::Microsoft::Management::Deployment::MatchResult> matches,
winrt::hresult extendedErrorCode);
#endif

winrt::Microsoft::Management::Deployment::FindPackagesResultStatus Status();
winrt::Windows::Foundation::Collections::IVectorView<winrt::Microsoft::Management::Deployment::MatchResult> Matches();
bool WasLimitExceeded();
winrt::hresult ExtendedErrorCode();

#if !defined(INCLUDE_ONLY_INTERFACE_METHODS)
private:
winrt::Microsoft::Management::Deployment::FindPackagesResultStatus m_status = winrt::Microsoft::Management::Deployment::FindPackagesResultStatus::Ok;
Windows::Foundation::Collections::IVector<winrt::Microsoft::Management::Deployment::MatchResult> m_matches{
winrt::single_threaded_vector<winrt::Microsoft::Management::Deployment::MatchResult>() };
bool m_wasLimitExceeded = false;
winrt::hresult m_extendedErrorCode = S_OK;
#endif
};
}
2 changes: 1 addition & 1 deletion src/Microsoft.Management.Deployment/PackageCatalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace winrt::Microsoft::Management::Deployment::implementation
winrt::Microsoft::Management::Deployment::implementation::FindPackagesResult>>();
// TODO: Add search timeout and error code.
winrt::Microsoft::Management::Deployment::FindPackagesResultStatus status = FindPackagesResultStatus(hr);
findPackagesResult->Initialize(status, isTruncated, matches);
findPackagesResult->Initialize(status, isTruncated, matches, hr);
return *findPackagesResult;
}

Expand Down
Loading

0 comments on commit 8f6b6f8

Please sign in to comment.