From 791040364b1b849506b7edc3794b4a812733636a Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Wed, 23 Jul 2025 19:37:51 -0700 Subject: [PATCH] Improves error reporting for failed FFU builds Refactors the error handling for failed build jobs to more reliably capture the root cause of the failure. The logic now uses `Receive-Job` with an `ErrorVariable` to directly capture the error stream from the job, which provides a more accurate terminating error message than the previous method. Additionally, the error message box now includes the full path to the log file, making it easier for the user to locate. --- FFUDevelopment/BuildFFUVM_UI.ps1 | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM_UI.ps1 b/FFUDevelopment/BuildFFUVM_UI.ps1 index 7badf2c..3be10c7 100644 --- a/FFUDevelopment/BuildFFUVM_UI.ps1 +++ b/FFUDevelopment/BuildFFUVM_UI.ps1 @@ -285,28 +285,30 @@ $script:uiState.Controls.btnRun.Add_Click({ $finalStatusText = "FFU build completed successfully." if ($currentJob.State -eq 'Failed') { - # Try to get a detailed error message. $reason = $null - if ($currentJob.JobStateInfo.Reason) { + + # Use Receive-Job with -ErrorVariable to reliably capture the error stream from the job, + # as suggested by the research on handling job errors. + Receive-Job -Job $currentJob -Keep -ErrorVariable jobErrors -ErrorAction SilentlyContinue | Out-Null + + if ($null -ne $jobErrors -and $jobErrors.Count -gt 0) { + # The terminating error is typically the last one in the stream. + $reason = ($jobErrors | Select-Object -Last 1).ToString() + } + + # If Receive-Job didn't surface an error, fall back to the JobStateInfo.Reason property. + if ([string]::IsNullOrWhiteSpace($reason) -and $currentJob.JobStateInfo.Reason) { $reason = $currentJob.JobStateInfo.Reason.Message } - # If the primary reason is empty, check the child job's error stream for more details. - if ([string]::IsNullOrWhiteSpace($reason) -and $currentJob.ChildJobs.Count -gt 0) { - $errorMessages = $currentJob.ChildJobs[0].Error | ForEach-Object { $_.ToString() } - if ($errorMessages) { - $reason = $errorMessages -join [System.Environment]::NewLine - } - } - - # Fallback if no specific reason is found + # Final fallback if no specific reason can be found. if ([string]::IsNullOrWhiteSpace($reason)) { $reason = "An unknown error occurred. The job failed without a specific reason." } $finalStatusText = "FFU build failed. Check FFUDevelopment.log for details." WriteLog "BuildFFUVM.ps1 job failed. Reason: $reason" - [System.Windows.MessageBox]::Show("The build process failed. Please check the log file for details.`n`nError: $reason", "Build Error", "OK", "Error") | Out-Null + [System.Windows.MessageBox]::Show("The build process failed. Please check the $FFUDevelopmentPath\FFUDevelopment.log file for details.`n`nError: $reason", "Build Error", "OK", "Error") | Out-Null $script:uiState.Controls.pbOverallProgress.Visibility = 'Collapsed' } else {