-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide error codes for enhancing error handling from clients (#927)
Previously, server sends `connect.Code` to clients to indicate error code, such as `FailedPrecondition`, in a simplistic manner. This makes it challenging for clients to differentiate and handle individual situations effectively. This commit provides error detailed codes as metadata for enhancing error handling from clients.
- Loading branch information
1 parent
28ef053
commit 1d96ea0
Showing
4 changed files
with
181 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package converter | ||
|
||
import ( | ||
"errors" | ||
|
||
"connectrpc.com/connect" | ||
"google.golang.org/genproto/googleapis/rpc/errdetails" | ||
) | ||
|
||
// ErrorCodeOf returns the error code of the given error. | ||
func ErrorCodeOf(err error) string { | ||
var connectErr *connect.Error | ||
if !errors.As(err, &connectErr) { | ||
return "" | ||
} | ||
for _, detail := range connectErr.Details() { | ||
msg, valueErr := detail.Value() | ||
if valueErr != nil { | ||
continue | ||
} | ||
|
||
if errorInfo, ok := msg.(*errdetails.ErrorInfo); ok { | ||
return errorInfo.GetMetadata()["code"] | ||
} | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.