Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error code and category funcs #109

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type Error struct {

// Tag is a string identifying the error, used with HTTP responses only.
tag string

// Category is a string identifying the category of the error, used for error code metrics only.
jake-engelberg marked this conversation as resolved.
Show resolved Hide resolved
category string
}

// ErrorBuilder is used to build the error
Expand Down Expand Up @@ -84,6 +87,22 @@ func (e *Error) GrpcStatusCode() grpcCodes.Code {
return e.grpcCode
}

// ErrorCode returns the error code from the error
func (e *Error) ErrorCode() string {
for _, detail := range e.details {
if _, ok := detail.(*errdetails.ErrorInfo); ok {
_, errorCode := convertErrorDetails(detail, *e)
return errorCode
}
}
return ""
}

// Category returns the error code's category
func (e *Error) Category() string {
return e.category
}

// Error implements the error interface.
func (e Error) Error() string {
return e.String()
Expand Down Expand Up @@ -334,14 +353,15 @@ ErrorBuilder
**************************************/

// NewBuilder create a new ErrorBuilder using the supplied required error fields
func NewBuilder(grpcCode grpcCodes.Code, httpCode int, message string, tag string) *ErrorBuilder {
func NewBuilder(grpcCode grpcCodes.Code, httpCode int, message string, tag string, category string) *ErrorBuilder {
return &ErrorBuilder{
err: Error{
details: make([]proto.Message, 0),
grpcCode: grpcCode,
httpCode: httpCode,
message: message,
tag: tag,
category: category,
},
}
}
Expand Down
Loading