$error is set to "Syntax error in the request which seems not conform to the OData protocol" when SAP returns error
Issue
When powerGate cmdlets like Get-ErpObject are used with SAP Gateway the $error variable is filled with "Syntax error in the request which seems not conform to the OData protocol" instead of the actual SAP error.
Solution
To get around this the actual SAP error message can be read from the RawResponse. This helper function
function Get-SapErrorMsg {
if ($global:error.Count -le 0) {
return
}
if ($global:error[0].Exception.InnerException) {
$errorMessage = $global:error[0].Exception.InnerException.Exception.Message
}
elseif ($global:error[0].Exception.RawResponse) {
$jsonResponseError = ConvertFrom-Json -InputObject $global:error[0].Exception.RawResponse
$errorMessage += $jsonResponseError.error.message.value
$jsonResponseError.error.innererror.errordetails | Where-Object { $_.severity -eq "error"} | ForEach-Object {
$errorMessage += $($_.message) + ";"
}
}
else {
$errorMessage = $global:error[0].Exception.Message
}
return $errorMessage
}