From a044c97aa9af9a34bdd62661ff13e73280379bfa Mon Sep 17 00:00:00 2001 From: Elena Kolevska Date: Fri, 19 Jan 2024 16:51:08 +0000 Subject: [PATCH] Docs for error handling (#482) * Updates docs with error handling example Signed-off-by: Elena Kolevska * Adds docs for handling handling error details Signed-off-by: Elena Kolevska * Updates main readme with example for handling error details Signed-off-by: Elena Kolevska --------- Signed-off-by: Elena Kolevska --- Readme.md | 44 +++++++++++++++++++ .../en/go-sdk-docs/go-client/_index.md | 42 ++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/Readme.md b/Readme.md index 1ccf4431..d5f16109 100644 --- a/Readme.md +++ b/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). diff --git a/daprdocs/content/en/go-sdk-docs/go-client/_index.md b/daprdocs/content/en/go-sdk-docs/go-client/_index.md index 5451d729..420df78a 100644 --- a/daprdocs/content/en/go-sdk-docs/go-client/_index.md +++ b/daprdocs/content/en/go-sdk-docs/go-client/_index.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