Skip to content

Commit

Permalink
support for errorpreference
Browse files Browse the repository at this point in the history
  • Loading branch information
jformacek committed Aug 16, 2023
1 parent d75f3ee commit 5616960
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
10 changes: 9 additions & 1 deletion Commands/BuildModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ param
[string]$rootPath = '.'
)
$moduleFile = "$rootPath\Module\CosmosLite\CosmosLite.psm1"
'#region Public commands' | Out-File -FilePath $moduleFile

'#region Definitions' | Out-File -FilePath $moduleFile
foreach($file in Get-ChildItem -Path "$rootPath\Commands\Definitions")
{
Get-Content $file.FullName | Out-File -FilePath $moduleFile -Append
}
'#endregion Definitions' | Out-File -FilePath $moduleFile -Append

'#region Public commands' | Out-File -FilePath $moduleFile -Append
foreach($file in Get-ChildItem -Path "$rootPath\Commands\Public")
{
Get-Content $file.FullName | Out-File -FilePath $moduleFile -Append
Expand Down
11 changes: 11 additions & 0 deletions Commands/Definitions/CosmosLiteException.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CosmosLiteException : Exception {
[string] $code

CosmosLiteException($Code, $Message) : base($Message) {
$this.Code = $code
}

[string] ToString() {
return "$($this.Code): $($this.Message)"
}
}
15 changes: 15 additions & 0 deletions Commands/Internal/ProcessCosmosResponseInternal.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ function ProcessCosmosResponseInternal
throw new-object System.FormatException("InvalidJsonPayloadReceived. Error: $($_.Exception.Message)`nPayload: $s")
}
}
if(-not $retVal['IsSuccess'])
{
$ex = [CosmosLiteException]::new($retVal['Data'].code, $retVal['Data'].message)
switch($ErrorActionPreference)
{
'Stop' {
throw $ex
break;
}
'Continue' {
Write-Error $ex
break;
}
}
}
[PSCustomObject]$retVal
}
}
5 changes: 3 additions & 2 deletions Commands/Public/Get-CosmosAccessToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ function Get-CosmosAccessToken
{
if([string]::IsNullOrEmpty($context))
{
throw "Call Connect-Cosmos first"
throw ([CosmosLiteException]::new('NotInitialized', 'Call Connect-Cosmos first'))
}

if($null -eq $context.AuthFactory)
{
throw "Call Connect-Cosmos first for CosmosDB account = $($context.AccountName)"
throw ([CosmosLiteException]::new('NotInitialized', "Call Connect-Cosmos first for CosmosDB account = $($context.AccountName)"))

}
#we specify scopes here in case that user pushes own factory without properly specified default scopes
Get-AadToken -Factory $context.AuthFactory -Scopes $context.RequiredScopes
Expand Down
Binary file modified Module/CosmosLite/CosmosLite.psd1
Binary file not shown.
33 changes: 31 additions & 2 deletions Module/CosmosLite/CosmosLite.psm1
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
#region Definitions
class CosmosLiteException : Exception {
[string] $code

CosmosLiteException($Code, $Message) : base($Message) {
$this.Code = $code
}

[string] ToString() {
return "$($this.Code): $($this.Message)"
}
}
#endregion Definitions
#region Public commands
function Connect-Cosmos
{
Expand Down Expand Up @@ -236,12 +249,13 @@ function Get-CosmosAccessToken
{
if([string]::IsNullOrEmpty($context))
{
throw "Call Connect-Cosmos first"
throw ([CosmosLiteException]::new('NotInitialized', 'Call Connect-Cosmos first'))
}

if($null -eq $context.AuthFactory)
{
throw "Call Connect-Cosmos first for CosmosDB account = $($context.AccountName)"
throw ([CosmosLiteException]::new('NotInitialized', "Call Connect-Cosmos first for CosmosDB account = $($context.AccountName)"))

}
#we specify scopes here in case that user pushes own factory without properly specified default scopes
Get-AadToken -Factory $context.AuthFactory -Scopes $context.RequiredScopes
Expand Down Expand Up @@ -1373,6 +1387,21 @@ function ProcessCosmosResponseInternal
throw new-object System.FormatException("InvalidJsonPayloadReceived. Error: $($_.Exception.Message)`nPayload: $s")
}
}
if(-not $retVal['IsSuccess'])
{
$ex = [CosmosLiteException]::new($retVal['Data'].code, $retVal['Data'].message)
switch($ErrorActionPreference)
{
'Stop' {
throw $ex
break;
}
'Continue' {
Write-Error $ex
break;
}
}
}
[PSCustomObject]$retVal
}
}
Expand Down

0 comments on commit 5616960

Please sign in to comment.