@@ -508,6 +508,19 @@ namespace AppInstaller::CLI::Workflow
508508 WriteJsonOutput (context, result);
509509 }
510510
511+ Resource::LocString GetUnexpectedErrorMessage (std::string_view message)
512+ {
513+ std::string result = Resource::LocString{ Resource::String::UnexpectedErrorExecutingCommand }.get ();
514+
515+ if (!message.empty ())
516+ {
517+ result += ' ' ;
518+ result += message;
519+ }
520+
521+ return Resource::LocString{ Utility::LocIndString{ std::move (result) } };
522+ }
523+
511524 void OutputInstalledPackagesJson (
512525 Execution::Context& context,
513526 std::vector<InstalledPackagesTableLine>& lines,
@@ -790,9 +803,17 @@ namespace AppInstaller::CLI::Workflow
790803 Logging::Telemetry ().LogException (Logging::FailureTypeEnum::ResultException, re.what ());
791804 if (context)
792805 {
793- context->Reporter .Error () <<
794- Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
795- GetUserPresentableMessage (re) << std::endl;
806+ auto message = GetUserPresentableMessage (re);
807+ if (IsJsonOutputFormat (context->Args ))
808+ {
809+ OutputInstalledPackagesJsonError (*context, re.GetErrorCode (), GetUnexpectedErrorMessage (message));
810+ }
811+ else
812+ {
813+ context->Reporter .Error () <<
814+ Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
815+ message << std::endl;
816+ }
796817 }
797818 return re.GetErrorCode ();
798819 }
@@ -802,9 +823,16 @@ namespace AppInstaller::CLI::Workflow
802823 Logging::Telemetry ().LogException (Logging::FailureTypeEnum::WinrtHResultError, message);
803824 if (context)
804825 {
805- context->Reporter .Error () <<
806- Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
807- message << std::endl;
826+ if (IsJsonOutputFormat (context->Args ))
827+ {
828+ OutputInstalledPackagesJsonError (*context, hre.code (), GetUnexpectedErrorMessage (message));
829+ }
830+ else
831+ {
832+ context->Reporter .Error () <<
833+ Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
834+ message << std::endl;
835+ }
808836 }
809837 return hre.code ();
810838 }
@@ -832,9 +860,17 @@ namespace AppInstaller::CLI::Workflow
832860 Logging::Telemetry ().LogException (Logging::FailureTypeEnum::StdException, e.what ());
833861 if (context)
834862 {
835- context->Reporter .Error () <<
836- Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
837- GetUserPresentableMessage (e) << std::endl;
863+ auto message = GetUserPresentableMessage (e);
864+ if (IsJsonOutputFormat (context->Args ))
865+ {
866+ OutputInstalledPackagesJsonError (*context, APPINSTALLER_CLI_ERROR_COMMAND_FAILED , GetUnexpectedErrorMessage (message));
867+ }
868+ else
869+ {
870+ context->Reporter .Error () <<
871+ Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
872+ message << std::endl;
873+ }
838874 }
839875 return APPINSTALLER_CLI_ERROR_COMMAND_FAILED ;
840876 }
@@ -844,8 +880,15 @@ namespace AppInstaller::CLI::Workflow
844880 Logging::Telemetry ().LogException (Logging::FailureTypeEnum::Unknown, {});
845881 if (context)
846882 {
847- context->Reporter .Error () <<
848- Resource::String::UnexpectedErrorExecutingCommand << " ???" _liv << std::endl;
883+ if (IsJsonOutputFormat (context->Args ))
884+ {
885+ OutputInstalledPackagesJsonError (*context, APPINSTALLER_CLI_ERROR_COMMAND_FAILED , GetUnexpectedErrorMessage (" ???" sv));
886+ }
887+ else
888+ {
889+ context->Reporter .Error () <<
890+ Resource::String::UnexpectedErrorExecutingCommand << " ???" _liv << std::endl;
891+ }
849892 }
850893 return APPINSTALLER_CLI_ERROR_COMMAND_FAILED ;
851894 }
0 commit comments