mirror of https://github.com/dapr/go-sdk.git
Docs for error handling (#482)
* Updates docs with error handling example Signed-off-by: Elena Kolevska <elena@kolevska.com> * Adds docs for handling handling error details Signed-off-by: Elena Kolevska <elena@kolevska.com> * Updates main readme with example for handling error details Signed-off-by: Elena Kolevska <elena@kolevska.com> --------- Signed-off-by: Elena Kolevska <elena@kolevska.com>
This commit is contained in:
parent
c8f3533787
commit
1ad973a458
44
Readme.md
44
Readme.md
|
@ -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).
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue