From 3b6b770bee7323be5cc2d7e0b52c80ff0e42a0b1 Mon Sep 17 00:00:00 2001 From: Shaun Lawrie Date: Wed, 23 Oct 2024 23:42:17 +1300 Subject: [PATCH] Enhance error handling for Spectre Console markup input in RenderableTransformationAttribute --- .../private/completions/Transformers.psm1 | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/PwshSpectreConsole/private/completions/Transformers.psm1 b/PwshSpectreConsole/private/completions/Transformers.psm1 index d2c9434e..7a2c1363 100644 --- a/PwshSpectreConsole/private/completions/Transformers.psm1 +++ b/PwshSpectreConsole/private/completions/Transformers.psm1 @@ -103,7 +103,18 @@ class RenderableTransformationAttribute : ArgumentTransformationAttribute { # For others just dump them as either strings formatted with markup which are easy to identify by the closing tag [/] or as plain text if ($InputData -like "*[/]*" -or $InputData -like "*:*:*") { - return [Spectre.Console.Markup]::new($InputData) + try { + $markup = [Spectre.Console.Markup]::new($InputData) + return $markup + } catch { + throw @( + "`n`nYour input includes Spectre Console markup characters (see https://spectreconsole.net/markup).", + "Escape the special characters in the input before using it in a Spectre Console function using the Get-SpectreEscapedText function.", + "", + " e.g. `$myEscapedInput = Get-SpectreEscapedText '$InputData'", + "`n" + ) -join "`n" + } } else { return [Spectre.Console.Text]::new(($InputData | Out-String -NoNewline)) }