Open insomniacc opened 1 year ago
The error on core comes from the response body nested under result. On 5.1 only the typical header error is shown not the response. It would be useful to parse these and display them correctly for both core and 5.1
Also needs checking all commands should throw a readable exception if there are permission/authorization errors.
I've forced an error in 5.1 (400 bad request), checking the full exception object from both invoke-webrequest and invoke-restmethod still doesnt show any further detail than this. Not sure what the difference between that and core is, with core I'm getting
{"error":{"message":"Pagination not supported","detail":"The requested query is too long to build the response pagination header URLs. Please do one of the following: shorten the sysparm_query, or query without pagination by setting the parameter 'sysparm_suppress_pagination_header' to true, or
set 'sysparm_limit' with a value larger then 621 to bypass the need for pagination."},"status":"failure"}
Under the ErrorDetails inside the exception. I think it's not going to be a straight forward one so I'll leave this for now.
Hey Tom 👋,
5.1 will stop short of reading the full response body when it gets an error and will instead just read the headers. If, as in the case of ServiceNow, those headers don't contain too much useful info - you can get to the full response stream and decode it yourself by doing something like:
try {
# ServiceNow Failing Rest Call Here
} catch {
$StreamReader = New-Object System.IO.StreamReader -argumentList ($_.Response.GetResponseStream());
$ErrorBody = $StreamReader.ReadToEnd()
$StatusCode = [int] $_.Exception.Response.StatusCode
$StatusDescription = $_.Exception.Response.StatusDescription + " " + $ErrorBody
}
I've not tested the above (it's from an old ServiceNow script of mine actually) but it should point you in the right direction.
Cheers
@liamjpeters Hey, thanks for this comment, interesting, I'll take a closer look this next week, haven't unfortunately had much chance of late! Hope things are going well!
If using core you'll get a relevant error from a bad rest request including the error message. example:
On 5.1 errors are basic and messages not displayed: example:
Enhancing the error handling and output would be a better user experience on 5.1