Skip to content

Commit 8f6b6f8

Browse files
authored
Add extended error codes to results without them (#4858)
## 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.
1 parent 8a31bfd commit 8f6b6f8

9 files changed

+159
-105
lines changed

src/AppInstallerCLICore/Workflows/WorkflowBase.cpp

+35-15
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ namespace AppInstaller::CLI::Workflow
388388
return authArgs;
389389
}
390390

391-
HRESULT HandleException(Execution::Context& context, std::exception_ptr exception)
391+
HRESULT HandleException(Execution::Context* context, std::exception_ptr exception)
392392
{
393393
try
394394
{
@@ -399,47 +399,67 @@ namespace AppInstaller::CLI::Workflow
399399
{
400400
// Even though they are logged at their source, log again here for completeness.
401401
Logging::Telemetry().LogException(Logging::FailureTypeEnum::ResultException, re.what());
402-
context.Reporter.Error() <<
403-
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
404-
GetUserPresentableMessage(re) << std::endl;
402+
if (context)
403+
{
404+
context->Reporter.Error() <<
405+
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
406+
GetUserPresentableMessage(re) << std::endl;
407+
}
405408
return re.GetErrorCode();
406409
}
407410
catch (const winrt::hresult_error& hre)
408411
{
409412
std::string message = GetUserPresentableMessage(hre);
410413
Logging::Telemetry().LogException(Logging::FailureTypeEnum::WinrtHResultError, message);
411-
context.Reporter.Error() <<
412-
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
413-
message << std::endl;
414+
if (context)
415+
{
416+
context->Reporter.Error() <<
417+
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
418+
message << std::endl;
419+
}
414420
return hre.code();
415421
}
416422
catch (const Settings::GroupPolicyException& e)
417423
{
418-
auto policy = Settings::TogglePolicy::GetPolicy(e.Policy());
419-
auto policyNameId = policy.PolicyName();
420-
context.Reporter.Error() << Resource::String::DisabledByGroupPolicy(policyNameId) << std::endl;
424+
if (context)
425+
{
426+
auto policy = Settings::TogglePolicy::GetPolicy(e.Policy());
427+
auto policyNameId = policy.PolicyName();
428+
context->Reporter.Error() << Resource::String::DisabledByGroupPolicy(policyNameId) << std::endl;
429+
}
421430
return APPINSTALLER_CLI_ERROR_BLOCKED_BY_POLICY;
422431
}
423432
catch (const std::exception& e)
424433
{
425434
Logging::Telemetry().LogException(Logging::FailureTypeEnum::StdException, e.what());
426-
context.Reporter.Error() <<
427-
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
428-
GetUserPresentableMessage(e) << std::endl;
435+
if (context)
436+
{
437+
context->Reporter.Error() <<
438+
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
439+
GetUserPresentableMessage(e) << std::endl;
440+
}
429441
return APPINSTALLER_CLI_ERROR_COMMAND_FAILED;
430442
}
431443
catch (...)
432444
{
433445
LOG_CAUGHT_EXCEPTION();
434446
Logging::Telemetry().LogException(Logging::FailureTypeEnum::Unknown, {});
435-
context.Reporter.Error() <<
436-
Resource::String::UnexpectedErrorExecutingCommand << " ???"_liv << std::endl;
447+
if (context)
448+
{
449+
context->Reporter.Error() <<
450+
Resource::String::UnexpectedErrorExecutingCommand << " ???"_liv << std::endl;
451+
}
437452
return APPINSTALLER_CLI_ERROR_COMMAND_FAILED;
438453
}
439454

440455
return E_UNEXPECTED;
441456
}
442457

458+
HRESULT HandleException(Execution::Context& context, std::exception_ptr exception)
459+
{
460+
return HandleException(&context, exception);
461+
}
462+
443463
void OpenSource::operator()(Execution::Context& context) const
444464
{
445465
std::string_view sourceName;

src/AppInstallerCLICore/Workflows/WorkflowBase.h

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ namespace AppInstaller::CLI::Workflow
8484
// Helper to create authentication arguments from context input.
8585
Authentication::AuthenticationArguments GetAuthenticationArguments(const Execution::Context& context);
8686

87+
// Helper to report exceptions and return the HRESULT.
88+
// If context is null, no output will be attempted.
89+
HRESULT HandleException(Execution::Context* context, std::exception_ptr exception);
90+
8791
// Helper to report exceptions and return the HRESULT.
8892
HRESULT HandleException(Execution::Context& context, std::exception_ptr exception);
8993

src/Microsoft.Management.Deployment/ConnectResult.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
namespace winrt::Microsoft::Management::Deployment::implementation
88
{
9-
void ConnectResult::Initialize(winrt::Microsoft::Management::Deployment::ConnectResultStatus status, winrt::Microsoft::Management::Deployment::PackageCatalog packageCatalog)
9+
void ConnectResult::Initialize(winrt::Microsoft::Management::Deployment::ConnectResultStatus status, winrt::Microsoft::Management::Deployment::PackageCatalog packageCatalog, winrt::hresult extendedErrorCode)
1010
{
1111
m_status = status;
12-
m_packageCatalog = packageCatalog;
12+
m_packageCatalog = packageCatalog;
13+
m_extendedErrorCode = extendedErrorCode;
1314
}
1415
winrt::Microsoft::Management::Deployment::ConnectResultStatus ConnectResult::Status()
1516
{
@@ -18,5 +19,10 @@ namespace winrt::Microsoft::Management::Deployment::implementation
1819
winrt::Microsoft::Management::Deployment::PackageCatalog ConnectResult::PackageCatalog()
1920
{
2021
return m_packageCatalog;
22+
}
23+
24+
winrt::hresult ConnectResult::ExtendedErrorCode()
25+
{
26+
return m_extendedErrorCode;
2127
}
2228
}

src/Microsoft.Management.Deployment/ConnectResult.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ namespace winrt::Microsoft::Management::Deployment::implementation
1010
ConnectResult() = default;
1111

1212
#if !defined(INCLUDE_ONLY_INTERFACE_METHODS)
13-
void Initialize(winrt::Microsoft::Management::Deployment::ConnectResultStatus status, winrt::Microsoft::Management::Deployment::PackageCatalog packageCatalog);
13+
void Initialize(winrt::Microsoft::Management::Deployment::ConnectResultStatus status, winrt::Microsoft::Management::Deployment::PackageCatalog packageCatalog, winrt::hresult extendedErrorCode);
1414
#endif
1515

1616
winrt::Microsoft::Management::Deployment::ConnectResultStatus Status();
1717
winrt::Microsoft::Management::Deployment::PackageCatalog PackageCatalog();
18+
winrt::hresult ExtendedErrorCode();
1819

1920
#if !defined(INCLUDE_ONLY_INTERFACE_METHODS)
2021
private:
2122
winrt::Microsoft::Management::Deployment::ConnectResultStatus m_status = winrt::Microsoft::Management::Deployment::ConnectResultStatus::Ok;
2223
winrt::Microsoft::Management::Deployment::PackageCatalog m_packageCatalog{ nullptr };
24+
winrt::hresult m_extendedErrorCode = S_OK;
2325
#endif
2426
};
2527
}

src/Microsoft.Management.Deployment/FindPackagesResult.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ namespace winrt::Microsoft::Management::Deployment::implementation
1010
void FindPackagesResult::Initialize(
1111
winrt::Microsoft::Management::Deployment::FindPackagesResultStatus status,
1212
bool wasLimitExceeded,
13-
Windows::Foundation::Collections::IVector<Microsoft::Management::Deployment::MatchResult> matches)
13+
Windows::Foundation::Collections::IVector<Microsoft::Management::Deployment::MatchResult> matches,
14+
winrt::hresult extendedErrorCode)
1415
{
1516
m_status = status;
1617
m_matches = matches;
1718
m_wasLimitExceeded = wasLimitExceeded;
19+
m_extendedErrorCode = extendedErrorCode;
1820
}
1921
winrt::Microsoft::Management::Deployment::FindPackagesResultStatus FindPackagesResult::Status()
2022
{
@@ -28,4 +30,9 @@ namespace winrt::Microsoft::Management::Deployment::implementation
2830
{
2931
return m_wasLimitExceeded;
3032
}
33+
34+
winrt::hresult FindPackagesResult::ExtendedErrorCode()
35+
{
36+
return m_extendedErrorCode;
37+
}
3138
}

src/Microsoft.Management.Deployment/FindPackagesResult.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ namespace winrt::Microsoft::Management::Deployment::implementation
1414
void Initialize(
1515
winrt::Microsoft::Management::Deployment::FindPackagesResultStatus status,
1616
bool wasLimitExceeded,
17-
Windows::Foundation::Collections::IVector<winrt::Microsoft::Management::Deployment::MatchResult> matches);
17+
Windows::Foundation::Collections::IVector<winrt::Microsoft::Management::Deployment::MatchResult> matches,
18+
winrt::hresult extendedErrorCode);
1819
#endif
1920

2021
winrt::Microsoft::Management::Deployment::FindPackagesResultStatus Status();
2122
winrt::Windows::Foundation::Collections::IVectorView<winrt::Microsoft::Management::Deployment::MatchResult> Matches();
2223
bool WasLimitExceeded();
24+
winrt::hresult ExtendedErrorCode();
2325

2426
#if !defined(INCLUDE_ONLY_INTERFACE_METHODS)
2527
private:
2628
winrt::Microsoft::Management::Deployment::FindPackagesResultStatus m_status = winrt::Microsoft::Management::Deployment::FindPackagesResultStatus::Ok;
2729
Windows::Foundation::Collections::IVector<winrt::Microsoft::Management::Deployment::MatchResult> m_matches{
2830
winrt::single_threaded_vector<winrt::Microsoft::Management::Deployment::MatchResult>() };
2931
bool m_wasLimitExceeded = false;
32+
winrt::hresult m_extendedErrorCode = S_OK;
3033
#endif
3134
};
3235
}

src/Microsoft.Management.Deployment/PackageCatalog.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ namespace winrt::Microsoft::Management::Deployment::implementation
115115
winrt::Microsoft::Management::Deployment::implementation::FindPackagesResult>>();
116116
// TODO: Add search timeout and error code.
117117
winrt::Microsoft::Management::Deployment::FindPackagesResultStatus status = FindPackagesResultStatus(hr);
118-
findPackagesResult->Initialize(status, isTruncated, matches);
118+
findPackagesResult->Initialize(status, isTruncated, matches, hr);
119119
return *findPackagesResult;
120120
}
121121

0 commit comments

Comments
 (0)