Automating stuff is easy but everytime I'll spend too much time ask myself what would be the best way to solve problems. Question like is this fast enough? Do I use too much memory? Is it secure or are these all options I've got?
I always end up in longer chats with a senior colleague which helps on my way. Nevertheless with my latest project (https://github.com/pheeling/dinotronicCMDB) I fell in love with exception handling.
The goal was to generate an execution report for the customer and speed up execution by exiting when no further steps are necessary. Normally I only had exception for certain code parts because I always though they where hard to code. This time around I found a good solution with one validation method containing a consolidated list of checks which ran for every needed verfication. Additionally I throw a specific exception which I catch and update for the customer report.
The following setup allows me to reduce complexity, speed execution and handle API input errors.
1. validation method
functionGet-XflexAssetManagement(){
return [XflexAssetManagement]::new()
}
class XflexAssetManagement {
.....
XflexAssetManagement (){
......
}
[Exception] validation($value){
if ([string]::IsNullOrEmpty($value) -or ($value-is [array])){
return Write-Error -Exception ([System.ArgumentNullException]::New()) -ErrorAction Stop
}
if (($value.BaseResponse.ResponseUri.AbsolutePath-eq"/api/matread") -and ($value.content-eq"[]")){
return Write-Error -Exception ([System.MissingMemberException]::New("Wrong material number"))
-ErrorAction Stop
}
if (($value.BaseResponse.ResponseUri.AbsolutePath-eq"/api/project") -and ($value.content-eq"[]")){
return Write-Error -Exception ([System.MissingMemberException]::New("Wrong project number"))
-ErrorAction Stop
}
if (($value.BaseResponse.ResponseUri.AbsolutePath-eq"/api/regread") -and ($value.content-eq"[]")){
return Write-Error -Exception ([System.MissingMemberException]::New("No Booking available under this material and project number")) -ErrorAction Stop
}
return $null
}
}
2. try / catch routing
try {
....
} catch [System.ArgumentNullException] {
Write-host $service.name
$response = [PSCustomObject]::new()
$response | Add-Member -MemberType noteproperty -Name Content -Value "$($PSitem.Exception)"-force
$service | Add-Member -MemberType NoteProperty -Name Response -Value @($response) -Force
$xflex.responseResults.Add($service)
"$(Get-Date) [Validation] $($service.name):$($service.response) Xflex API Error please check $Global:XflexInterfaceLog for Status" >> $Global:logFile
Remove-Variable -Name response
#Get-NewErrorHandling "Xflex API Severe Error" $PSitem
} catch [System.MissingMemberException] {
....
} catch {
"$(Get-Date) [Unknown] $($PSitem.Exception)" >> $Global:logFile}
The first catch routine is the prototype and adds the error message to my temporary variable which I later can use to build my report. As well I used Write-Error instead of throw.
I had difficulties defining a method and include a throw mechanism with different exceptions types. Additionally I needed to create terminating errors to enhance efficiency.
To have a better view and how to do this check out my github site.
Good reading source:
Commentaires