From c070df12e86f1dba6777ea941967eab4afaf98fa Mon Sep 17 00:00:00 2001 From: Alexander Petric Date: Tue, 8 Oct 2024 23:20:46 -0400 Subject: [PATCH] improving pwsh error handling (#4504) * making pwsh error handling more robust * cargo fmt --- backend/windmill-worker/src/bash_executor.rs | 25 +++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/backend/windmill-worker/src/bash_executor.rs b/backend/windmill-worker/src/bash_executor.rs index 1b1fc65f17..128eb7d25f 100644 --- a/backend/windmill-worker/src/bash_executor.rs +++ b/backend/windmill-worker/src/bash_executor.rs @@ -318,15 +318,33 @@ $env:PSModulePath = \"{};$PSModulePathBackup\"", POWERSHELL_CACHE_DIR ); + // NOTE: powershell error handling / termination is quite tricky compared to bash + // here we're trying to catch terminating errors and propagate the exit code + // to the caller such that the job will be marked as failed. It's up to the user + // to catch specific errors in their script not caught by the below as there is no + // generic set -eu as in bash + let strict_termination_start = "$ErrorActionPreference = 'Stop'\n\ + Set-StrictMode -Version Latest\n\ + try {\n"; + + let strict_termination_end = "\n\ + } catch {\n\ + Write-Output \"An error occurred:\n\"\ + Write-Output $_ + exit 1\n\ + }\n"; + // make sure param() is first let param_match = windmill_parser_bash::RE_POWERSHELL_PARAM.find(&content); let content: String = if let Some(param_match) = param_match { let param_match = param_match.as_str(); format!( - "{}\n{}\n{}", + "{}\n{}\n{}\n{}\n{}", param_match, profile, - content.replace(param_match, "") + strict_termination_start, + content.replace(param_match, ""), + strict_termination_end ) } else { format!("{}\n{}", profile, content) @@ -351,7 +369,8 @@ $env:PSModulePath = \"{};$PSModulePathBackup\"", $pipe = New-TemporaryFile\n\ & \"{}\" -File ./main.ps1 @args 2>&1 | Tee-Object -FilePath $pipe\n\ Get-Content -Path $pipe | Select-Object -Last 1 | Set-Content -Path './result2.out'\n\ - Remove-Item $pipe\n", + Remove-Item $pipe\n\ + exit $LASTEXITCODE\n", POWERSHELL_PATH.as_str() ), )?;