Skip to content

Commit

Permalink
Upgrade.md: Add before example for error handling (#265)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <[email protected]>
  • Loading branch information
Jakob3xD authored Mar 23, 2023
1 parent 2d469d4 commit 34ed92d
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

`SnapshotDeleteRequest` and `SnapshotDelete` changed the argument `Snapshot` type from `string` to `[]string`.

Before:
Before 3.0.0:

```go
// If you have a string containing your snapshot
Expand All @@ -29,7 +29,7 @@ reqSnapshots := &opensearchapi.SnapshotDeleteRequest{
}
```

After:
With 3.0.0:

```go
// If you have a string containing your snapshots
Expand All @@ -54,7 +54,24 @@ Prior versions only returned an error if the request failed to execute. For exam
With opensearch-go >= 3.0.0 each opensearchapi requests will return an error if the response http status code is > 299.
The error can be parsed into the new `opensearchapi.Error` type by using `errors.As` to match for exceptions and get a more detailed view.
Example:
Before 3.0.0:
```go
createIndex := opensearchapi.IndicesCreateRequest{
Index: IndexName,
Body: mapping,
}

ctx := context.Background()
createIndexResp, err := createIndex.Do(ctx, client)
if err != nil {
return err
}
if createIndexResp.IsError() {
fmt.Errorf("Opensearch returned an error. Status: %d", createIndexResp.StatusCode)
}
```
With 3.0.0:
```go
createIndex := opensearchapi.IndicesCreateRequest{
Expand All @@ -66,14 +83,15 @@ ctx := context.Background()
var opensearchError *opensearchapi.Error

createIndexResponse, err := createIndex.Do(ctx, client)
// Load err into opensearchapi.Error to access the fields and tolerate if the index already exists
if err != nil {
if errors.As(err, &opensearchError) {
if opensearchError.Err.Type != "resource_already_exists_exception" {
return err
}
} else {
// Load err into opensearchapi.Error to access the fields and tolerate if the index already exists
if err != nil {
if errors.As(err, &opensearchError) {
if opensearchError.Err.Type != "resource_already_exists_exception" {
return err
}
} else {
return err
}
}
```

0 comments on commit 34ed92d

Please sign in to comment.