Skip to content

Commit ade5ffa

Browse files
tksh164kpoineal
andauthored
feat: Add better exception experience to Start-WARAAnalyzer (#187)
* Change ErrorActionPreference to Stop * Add trap for entire script * Simplify parameter check for JSONFile * Simplify parameter check for ExpertAnalysisFile --------- Co-authored-by: Kyle Poineal <38540295+kpoineal@users.noreply.github.com>
1 parent 7c5c0e1 commit ade5ffa

1 file changed

Lines changed: 69 additions & 39 deletions

File tree

src/modules/wara/analyzer/2_wara_data_analyzer.ps1

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,52 +26,82 @@ Path to the Expert-Analysis file to be customized by script.
2626
https://github.com/Azure/Azure-Proactive-Resiliency-Library-v2
2727
#>
2828

29-
Param(
30-
[ValidatePattern('^https:\/\/.+$')]
31-
[string] $RecommendationDataUri = 'https://azure.github.io/WARA-Build/objects/recommendations.json',
32-
[string] $CustomRecommendationObject,
33-
[Parameter(mandatory = $true)]
34-
[string] $JSONFile,
35-
[string] $ExpertAnalysisFile
29+
param (
30+
[Parameter(Mandatory = $false)]
31+
[ValidatePattern('^https:\/\/.+$')]
32+
[string] $RecommendationDataUri = 'https://azure.github.io/WARA-Build/objects/recommendations.json',
33+
34+
[Parameter(Mandatory = $false)]
35+
[string] $CustomRecommendationObject,
36+
37+
[Parameter(Mandatory = $true)]
38+
[string] $JSONFile,
39+
40+
[Parameter(Mandatory = $false)]
41+
[string] $ExpertAnalysisFile = (Join-Path -Path $PSScriptRoot -ChildPath 'Expert-Analysis-v1.xlsx')
3642
)
3743

38-
# WARA In Scope Resource Types CSV File
39-
$RecommendationResourceTypesUri = 'https://azure.github.io/WARA-Build/objects/WARAinScopeResTypes.csv'
44+
$ErrorActionPreference = 'Stop'
45+
46+
trap {
47+
$ex = $_.Exception
48+
$horizontalLineLength = 40
49+
50+
$builder = New-Object -TypeName 'System.Text.StringBuilder'
51+
[void] $builder.AppendLine('')
52+
[void] $builder.AppendLine($ex.Message)
53+
[void] $builder.AppendLine('')
54+
55+
[void] $builder.AppendLine('*' * $horizontalLineLength)
56+
[void] $builder.AppendLine('Exception : ' + $ex.GetType().FullName)
57+
[void] $builder.AppendLine('FullyQualifiedErrorId : ' + $_.FullyQualifiedErrorId)
58+
[void] $builder.AppendLine('ErrorDetailsMessage : ' + $_.ErrorDetails.Message)
59+
[void] $builder.AppendLine('CategoryInfo : ' + $_.CategoryInfo.ToString())
60+
[void] $builder.AppendLine('StackTrace in PowerShell :')
61+
[void] $builder.AppendLine($_.ScriptStackTrace)
62+
63+
[void] $builder.AppendLine('')
64+
[void] $builder.AppendLine('---- Exception Details ----')
65+
[void] $builder.AppendLine('Exception : ' + $ex.GetType().FullName)
66+
[void] $builder.AppendLine('Message : ' + $ex.Message)
67+
[void] $builder.AppendLine('Source : ' + $ex.Source)
68+
[void] $builder.AppendLine('HResult : ' + $ex.HResult)
69+
[void] $builder.AppendLine('StackTrace :')
70+
[void] $builder.AppendLine($ex.StackTrace)
71+
72+
$depth = 1
73+
while ($ex.InnerException) {
74+
$ex = $ex.InnerException
75+
[void] $builder.AppendLine('')
76+
[void] $builder.AppendLine('---- Inner Exception Details {0} ----' -f $depth)
77+
[void] $builder.AppendLine('Exception : ' + $ex.GetType().FullName)
78+
[void] $builder.AppendLine('Message : ' + $ex.Message)
79+
[void] $builder.AppendLine('Source : ' + $ex.Source)
80+
[void] $builder.AppendLine('HResult : ' + $ex.HResult)
81+
[void] $builder.AppendLine('StackTrace :')
82+
[void] $builder.AppendLine($ex.StackTrace)
83+
$depth++
84+
}
4085

41-
# Check if the Expert-Analysis file exists
42-
$ExpertAnalysisPath = $PSScriptRoot + '\Expert-Analysis-v1.xlsx'
86+
[void] $builder.AppendLine('*' * $horizontalLineLength)
87+
$builder.ToString() | Write-Host -ForegroundColor Yellow
4388

44-
if (!$ExpertAnalysisFile)
45-
{
46-
Write-Debug ((get-date -Format 'yyyy-MM-dd HH:mm:ss') + (' - Testing: ' + './Expert-Analysis-v1.xlsx'))
47-
if ((Test-Path -Path ($ExpertAnalysisPath) -PathType Leaf) -eq $true) {
48-
$ExpertAnalysisFile = $ExpertAnalysisPath
49-
}
50-
}
51-
else
52-
{
53-
Throw "Error locating the Expert-Analysis file. Please provide a valid path to the Expert-Analysis file or reinstall the WARA Module."
54-
Exit
89+
break
5590
}
5691

57-
if ((Test-Path -Path $ExpertAnalysisFile -PathType Leaf) -eq $true) {
58-
$ExpertAnalysisFile = (Resolve-Path -Path $ExpertAnalysisFile).Path
59-
}
60-
else
61-
{
62-
Throw "The Expert-Analysis file does not exist. Please provide a valid path to the Expert-Analysis file."
63-
Exit
64-
}
92+
# WARA In Scope Resource Types CSV File
93+
$RecommendationResourceTypesUri = 'https://azure.github.io/WARA-Build/objects/WARAinScopeResTypes.csv'
6594

95+
# Verify the ExpertAnalysisFile parameter
96+
$expertAnalysisFilePath = (Resolve-Path -LiteralPath $ExpertAnalysisFile).Path
97+
if (-not (Test-Path -PathType Leaf -LiteralPath $expertAnalysisFilePath)) {
98+
throw 'The specified Expert Analysis file "{0}" is not a file. Please provide the path to the Expert Analysis file.' -f $expertAnalysisFilePath
99+
}
66100

67101
# Check if the JSON file exists
68-
if ((Test-Path -Path $JSONFile -PathType Leaf) -eq $true) {
69-
$JSONFile = (Resolve-Path -Path $JSONFile).Path
70-
}
71-
else
72-
{
73-
Throw "JSON file not found. Please provide a valid path to the JSON file."
74-
Exit
102+
$jsonFilePath = (Resolve-Path -LiteralPath $JSONFile).Path
103+
if (-not (Test-Path -PathType Leaf -LiteralPath $jsonFilePath)) {
104+
throw 'The specified JSON file "{0}" is not a file. Please provide the path to the JSON file.' -f $jsonFilePath
75105
}
76106

77107
$TableStyle = 'Light19'
@@ -1158,7 +1188,7 @@ Write-Debug ((get-date -Format 'yyyy-MM-dd HH:mm:ss') + ' - Invoking Function: R
11581188
Test-Requirement
11591189

11601190
Write-Debug ((get-date -Format 'yyyy-MM-dd HH:mm:ss') + ' - Invoking Function: Read-JSONFile')
1161-
$JSONContent = Read-JSONFile -JSONFile $JSONFile
1191+
$JSONContent = Read-JSONFile -JSONFile $jsonFilePath
11621192

11631193
Write-Debug ((get-date -Format 'yyyy-MM-dd HH:mm:ss') + ' - Importing Supported Types')
11641194
# Importing the CSV files to get the supported types and the friendly names for the resource types in the Retirements
@@ -1169,7 +1199,7 @@ Write-Host 'Analysing Excel File Template'
11691199

11701200
#$NewExpertAnalysisFile = Save-WARAExcelFile -ExpertAnalysisFile $ExpertAnalysisFile
11711201

1172-
$ExpertAnalysisTemplate = Open-ExcelPackage -Path $ExpertAnalysisFile
1202+
$ExpertAnalysisTemplate = Open-ExcelPackage -Path $expertAnalysisFilePath
11731203

11741204
Write-Debug ((get-date -Format 'yyyy-MM-dd HH:mm:ss') + ' - Invoking Function: Initialize-WARAImpactedResources')
11751205
# Creating the Array with the Impacted Resources to be added to the Excel file

0 commit comments

Comments
 (0)