Skip to content

Commit

Permalink
Add error handling sections for go and powershell
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-mairose-sp committed Dec 1, 2023
1 parent ac74aa0 commit 5b47a0d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
39 changes: 32 additions & 7 deletions products/idn/tools/sdk/go.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,11 @@ func main() {

resp, r, err := sailpoint.PaginateWithDefaults[v3.Account](apiClient.V3.AccountsApi.ListAccounts(ctx))
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `AccountsApi.ListAccount``: %v\n", err)
fmt.Fprintf(os.Stderr, "Error when calling `PaginateWithDefaults[v3.Account]``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
// response from `ListAccounts`: []Account
fmt.Fprintf(os.Stdout, "First response from `AccountsApi.ListAccount`: %v\n", resp[0].Name)
fmt.Fprintf(os.Stdout, "First response from `PaginateWithDefaults[v3.Account]`: %v\n", resp[0].Name)
}
```

Expand Down Expand Up @@ -296,11 +296,11 @@ func main() {

resp, r, err := sailpoint.Paginate[v3.Account](apiClient.V3.AccountsApi.ListAccounts(ctx), 0, 250, 150000)
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `AccountsApi.ListAccount``: %v\n", err)
fmt.Fprintf(os.Stderr, "Error when calling `Paginate[v3.Account]``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
// response from `ListAccounts`: []Account
fmt.Fprintf(os.Stdout, "First response from `AccountsApi.ListAccount`: %v\n", resp[0].Name)
fmt.Fprintf(os.Stdout, "First response from `Paginate[v3.Account]`: %v\n", resp[0].Name)
}
```

Expand Down Expand Up @@ -330,7 +330,7 @@ func getSearchResults(ctx context.Context, apiClient *sailpoint.APIClient) {
search.UnmarshalJSON(searchString)
resp, r, err := sailpoint.PaginateSearchApi(ctx, apiClient, *search, 0, 10, 10000)
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `AccountsApi.ListAccount``: %v\n", err)
fmt.Fprintf(os.Stderr, "Error when calling `PaginateSearchApi``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
// response from `search`
Expand Down Expand Up @@ -411,12 +411,37 @@ func main() {

resp, r, err := apiClient.V3.TransformsApi.ListTransforms(ctx).Filters("This is an incorrect string").Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `AccountsApi.ListAccount``: %v\n", err)
fmt.Fprintf(os.Stderr, "Error when calling `TransformsApi.ListTransforms``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
// response from `ListAccounts`: []Account
fmt.Fprintf(os.Stdout, "First response from `AccountsApi.ListAccount`: %v\n", resp)
fmt.Fprintf(os.Stdout, "First response from `TransformsApi.ListTransforms`: %v\n", resp)

}
```

### Error Handling

The SDK returns the response object, request and error object for each method. The error object will be defined for any response status that falls out of the range of 2xx.

Using an if statement we can check if there are any errors with the request and take actions on the results, such as logging the message or performing additional actions before exiting the program.

```go
resp, r, err := apiClient.V3.TransformsApi.ListTransforms(ctx).Filters("This is an incorrect string").Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `TransformsApi.ListTransforms``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
```

The program will continue running unless you specify to end it. Use `os.Exit(0)` within the error check to stop execution after an error is found.

```go
resp, r, err := apiClient.V3.TransformsApi.ListTransforms(ctx).Filters("This is an incorrect string").Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `TransformsApi.ListTransforms``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
os.Exit(0)
}
```

Expand Down
49 changes: 49 additions & 0 deletions products/idn/tools/sdk/powershell.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ Running `Get-Transforms` will return a list of all transforms in your tenant.

Running `Get-Transforms -Limit 10 -Filter 'name sw Test"'` will return a list of no more than 10 transforms whose names start with `Test`.

### WithHttpInfo Switch

By default, the cmdlets return just the response from the API without including any information about status code or headers returned. Use the `-WithHttpInfo` switch to return this information with the response.

```powershell
Get-Transforms -WithHttpInfo
Name Value
---- -----
Headers {[Date, System.String[]], [Transfer-Encoding, System.String[]], [Connection, System.String[]], [Server, System.String[]]…}
StatusCode 200
Response {System.Management.Automation.OrderedHashtable, System.Management.Automation.OrderedHashtable, System.Management.Automation.Ordered…
```

### Paginate Results

By default, your requests will return a maximum of 250 records. To return more, you must implement pagination. To learn more about pagination, refer to [Paginating Results](/idn/api/standard-collection-parameters/#paginating-results).
Expand Down Expand Up @@ -429,6 +443,41 @@ The following code will tell the SDK to retry 2 times after an unexpected error
Set-DefaultConfiguration -MaximumRetryCount 2 -RetryIntervalSeconds 3
```

### Error Handling

The SDK uses the Invoke-WebRequest cmdlet to handle HTTP requests. Invoke-WebRequest will throw a terminating error for any response that falls out of the range of 2xx. A non-2xx response will immediately halt the program and produce a stack trace.

Using a try/catch function, you can intercept any non success response and take actions on the results, such as logging the message or performing additional actions before exiting the program.

```powershell
# Catch any non 2xx response and log the status code and error message
try {
Get-Transforms -Filters "id eq"
}
catch {
Write-Host $_.Exception.Response.StatusCode.value__
Write-Host $_.ErrorDetails
}
```

Using the catch block will handle the error and the script execution will continue. If you wish to stop the scripts execution, include an `Exit` in the catch block.

In the following code, the `Get-AccessProfiles` cmdlet will never be called.

```powershell
# Catch any non 2xx response and log the status code and error message. Stop the script with the Exit keyword.
try {
Get-Transforms -Filters "id eq"
}
catch {
Write-Host $_.Exception.Response.StatusCode.value__
Write-Host $_.ErrorDetails
Exit
}
Get-AccessProfiles
```

## Discuss

You can use this SDK to build new tools that extend your IDN platform and improve experiences across your organization. Use this guide to get started, and if you have questions, don't hesitate to reach out on the SailPoint Developer Community forum at https://developer.sailpoint.com/discuss!

0 comments on commit 5b47a0d

Please sign in to comment.