Skip to content

Commit

Permalink
Docs for error handling (dapr#482)
Browse files Browse the repository at this point in the history
* Updates docs with error handling example

Signed-off-by: Elena Kolevska <[email protected]>

* Adds docs for handling handling error details

Signed-off-by: Elena Kolevska <[email protected]>

* Updates main readme with example for handling error details

Signed-off-by: Elena Kolevska <[email protected]>

---------

Signed-off-by: Elena Kolevska <[email protected]>
  • Loading branch information
elena-kolevska authored and mikeee committed Jan 19, 2024
1 parent 1020e13 commit a044c97
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,50 @@ func main() {
}
```

##### Error handling

Dapr errors are based on [gRPC's richer error model](https://cloud.google.com/apis/design/errors#error_model).
The following code shows how to parse and handle the error details:

```go
if err != nil {
st := status.Convert(err)

fmt.Printf("Code: %s\n", st.Code().String())
fmt.Printf("Message: %s\n", st.Message())

for _, detail := range st.Details() {
switch t := detail.(type) {
case *errdetails.ErrorInfo:
// Handle ErrorInfo details
fmt.Printf("ErrorInfo:\n- Domain: %s\n- Reason: %s\n- Metadata: %v\n", t.GetDomain(), t.GetReason(), t.GetMetadata())
case *errdetails.BadRequest:
// Handle BadRequest details
fmt.Println("BadRequest:")
for _, violation := range t.GetFieldViolations() {
fmt.Printf("- Key: %s\n", violation.GetField())
fmt.Printf("- The %q field was wrong: %s\n", violation.GetField(), violation.GetDescription())
}
case *errdetails.ResourceInfo:
// Handle ResourceInfo details
fmt.Printf("ResourceInfo:\n- Resource type: %s\n- Resource name: %s\n- Owner: %s\n- Description: %s\n",
t.GetResourceType(), t.GetResourceName(), t.GetOwner(), t.GetDescription())
case *errdetails.Help:
// Handle ResourceInfo details
fmt.Println("HelpInfo:")
for _, link := range t.GetLinks() {
fmt.Printf("- Url: %s\n", link.Url)
fmt.Printf("- Description: %s\n", link.Description)
}

default:
// Add cases for other types of details you expect
fmt.Printf("Unhandled error detail type: %v\n", t)
}
}
}
```

### Service (callback)

In addition to the client capabilities that allow you to call into the Dapr API, the Go SDK also provides `service` package to help you bootstrap Dapr callback services in either gRPC or HTTP. Instructions on how to use it are located [here](./service/Readme.md).
Expand Down
42 changes: 42 additions & 0 deletions daprdocs/content/en/go-sdk-docs/go-client/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,48 @@ The Dapr client package allows you to interact with other Dapr applications from
```go
import "github.com/dapr/go-sdk/client"
```
## Error handling
Dapr errors are based on [gRPC's richer error model](https://cloud.google.com/apis/design/errors#error_model).
The following code shows an example of how you can parse and handle the error details:

```go
if err != nil {
st := status.Convert(err)

fmt.Printf("Code: %s\n", st.Code().String())
fmt.Printf("Message: %s\n", st.Message())

for _, detail := range st.Details() {
switch t := detail.(type) {
case *errdetails.ErrorInfo:
// Handle ErrorInfo details
fmt.Printf("ErrorInfo:\n- Domain: %s\n- Reason: %s\n- Metadata: %v\n", t.GetDomain(), t.GetReason(), t.GetMetadata())
case *errdetails.BadRequest:
// Handle BadRequest details
fmt.Println("BadRequest:")
for _, violation := range t.GetFieldViolations() {
fmt.Printf("- Key: %s\n", violation.GetField())
fmt.Printf("- The %q field was wrong: %s\n", violation.GetField(), violation.GetDescription())
}
case *errdetails.ResourceInfo:
// Handle ResourceInfo details
fmt.Printf("ResourceInfo:\n- Resource type: %s\n- Resource name: %s\n- Owner: %s\n- Description: %s\n",
t.GetResourceType(), t.GetResourceName(), t.GetOwner(), t.GetDescription())
case *errdetails.Help:
// Handle ResourceInfo details
fmt.Println("HelpInfo:")
for _, link := range t.GetLinks() {
fmt.Printf("- Url: %s\n", link.Url)
fmt.Printf("- Description: %s\n", link.Description)
}

default:
// Add cases for other types of details you expect
fmt.Printf("Unhandled error detail type: %v\n", t)
}
}
}
```

## Building blocks

Expand Down

0 comments on commit a044c97

Please sign in to comment.