Skip to content

Commit

Permalink
feat: extend grain error response
Browse files Browse the repository at this point in the history
  • Loading branch information
qazwsxedckll committed Jan 19, 2024
1 parent b5d0285 commit 472713b
Show file tree
Hide file tree
Showing 30 changed files with 1,775 additions and 122 deletions.
82 changes: 82 additions & 0 deletions cluster/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cluster

import (
"errors"
"fmt"
)

const (
ErrorReason_OK = "OK"
ErrorReason_CANCELLED = "CANCELLED"
ErrorReason_UNKNOWN = "UNKNOWN"
ErrorReason_INVALID_ARGUMENT = "INVALID_ARGUMENT"
ErrorReason_DEADLINE_EXCEEDED = "DEADLINE_EXCEEDED"
ErrorReason_NOT_FOUND = "NOT_FOUND"
ErrorReason_ALREADY_EXISTS = "ALREADY_EXISTS"
ErrorReason_PERMISSION_DENIED = "PERMISSION_DENIED"
ErrorReason_RESOURCE_EXHAUSTED = "RESOURCE_EXHAUSTED"
ErrorReason_FAILED_PRECONDITION = "FAILED_PRECONDITION"
ErrorReason_ABORTED = "ABORTED"
ErrorReason_OUT_OF_RANGE = "OUT_OF_RANGE"
ErrorReason_UNIMPLEMENTED = "UNIMPLEMENTED"
ErrorReason_INTERNAL = "INTERNAL"
ErrorReason_UNAVAILABLE = "UNAVAILABLE"
ErrorReason_DATA_LOSS = "DATA_LOSS"
ErrorReason_UNAUTHENTICATED = "UNAUTHENTICATED"
)

func NewGrainErrorResponse(reason, message string) *GrainErrorResponse {
return &GrainErrorResponse{
Reason: reason,
Message: message,
}
}

func NewGrainErrorResponsef(reason, format string, args ...interface{}) *GrainErrorResponse {
return &GrainErrorResponse{
Reason: reason,
Message: fmt.Sprintf(format, args...),
}
}

func (m *GrainErrorResponse) Error() string {
return fmt.Sprintf("grain error response, reason: %s, message: %s, metadata: %v", m.Reason, m.Message, m.Metadata)
}

func (m *GrainErrorResponse) Is(err error) bool {
if e := new(GrainErrorResponse); errors.As(err, &e) {
return e.Reason == m.Reason
}
return false
}

func (m *GrainErrorResponse) Errorf(format string, args ...interface{}) error {
return NewGrainErrorResponse(m.Reason, fmt.Sprintf(format, args...))
}

func (m *GrainErrorResponse) WithMetadata(metadata map[string]string) *GrainErrorResponse {
m.Metadata = metadata
return m
}

func Reason(err error) string {
if err == nil {
return ErrorReason_UNKNOWN
}
return FromError(err).Reason
}

func FromError(err error) *GrainErrorResponse {
if err == nil {
return nil
}
if e := new(GrainErrorResponse); errors.As(err, &e) {
return e
}

ret := NewGrainErrorResponse(
ErrorReason_UNKNOWN,
err.Error(),
)
return ret
}
62 changes: 45 additions & 17 deletions cluster/grain.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions cluster/grain.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ message GrainResponse {
}

message GrainErrorResponse {
string err = 1;
}
string reason = 1;
string message = 2;
map<string, string> metadata = 3;
};
Loading

0 comments on commit 472713b

Please sign in to comment.