New-PSUAPIResponse & Throw

Product: PowerShell Universal
Version: 3.7.14

We have a bunch of Rest APIs that are consuming the same powershell module, allowing them to copy data from Prod to Test in a specific way.

Our modules use try / catch to stop executing the code if something breaks, and then does a throw printing the error message to a logfile on the PSU server - this parts works perfectly.

Our clients are requesting that this data is also shown to them, so i looked into the New-PSUAPIResponse cmdlet, hoping that it would solve the issue.

I tried it with this:

function Copy-Database123 {
    try {
        Import-Module -Name "DummyModuleThatDontExists" -ErrorAction stop -WarningAction stop
    }
    catch {
        New-PSUApiResponse -StatusCode 500 -Body "Failed!"
        throw "Failed! - stop here"
    }
}
Copy-Database123
New-Item -Name "PSU.txt" -Path C:\temp -ItemType File

The code stops as expected, but all i get is a “Invoke-RestMethod : The remote server returned an error: (400) Bad Request”

If i exclude Throw:

function Copy-Database123 {
    try {
        Import-Module -Name "DummyModuleThatDontExists" -ErrorAction stop -WarningAction stop
    }
    catch {
        New-PSUApiResponse -StatusCode 500 -Body "Failed!"
        #throw "Failed! - stop here"
    }
}
Copy-Database123
New-Item -Name "PSU.txt" -Path C:\temp -ItemType File

Then the return message is “Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error” - BUT the code continues to run (creating the PSU.txt file).

How do i return the $_.Exception.Message in the New-PSUAPIResponse & stop the code from executing after that?

Try this.

function Copy-Database123 {
    try {
        Import-Module -Name "DummyModuleThatDontExists" -ErrorAction stop -WarningAction stop
    }
    catch {
        New-PSUApiResponse -StatusCode 500 -Body $_.Exception.Message
        return
    }
}
Copy-Database123
New-Item -Name "PSU.txt" -Path C:\temp -ItemType File

That sadly didnt work.

I deleted the psu.txt and ran it again, and on the first try it returns an error 500, but also creates the file.
On the second go it returns an error 400, because the PSU.txt file already exists.

So its like it dosent care, about the function

Oh sorry. I didn’t even notice the function. Something like this should work.

function Copy-Database123 {
    try {
        Import-Module -Name "DummyModuleThatDontExists" -ErrorAction stop -WarningAction stop
    }
    catch {
        New-PSUApiResponse -StatusCode 500 -Body $_.Exception.Message
    }
}
$Response = Copy-Database123
if ($Response) {
    return $Response
}
New-Item -Name "PSU.txt" -Path C:\temp -ItemType File

Thanks! :slight_smile:
But that means none of my other cmdlets can generate any output, otherwise it would trigger the if ($response) - correct?

Like this for example:

function Copy-Database123 {
    try {
        Import-Module -Name "DummyModuleThatDontExists" -ErrorAction stop -WarningAction stop
    }
    catch {
        New-PSUApiResponse -StatusCode 500 -Body $_.Exception.Message
    }

    Get-DbaDatabase 

}
$Response = Copy-Database123
if ($Response) {
    return $Response
}
New-Item -Name "PSU.txt" -Path C:\temp -ItemType File

If Get-DbaDatabase generated some output, i would the entire response back, from all the functions that generates output.

Isnt there a way to use New-PSUApiResponse like a throw - so that is stops the execution, and sends the message?