github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/cluster/errors.go (about) 1 package cluster 2 3 import ( 4 "errors" 5 "fmt" 6 ) 7 8 const ( 9 ErrorReason_OK = "OK" 10 ErrorReason_CANCELLED = "CANCELLED" 11 ErrorReason_UNKNOWN = "UNKNOWN" 12 ErrorReason_INVALID_ARGUMENT = "INVALID_ARGUMENT" 13 ErrorReason_DEADLINE_EXCEEDED = "DEADLINE_EXCEEDED" 14 ErrorReason_NOT_FOUND = "NOT_FOUND" 15 ErrorReason_ALREADY_EXISTS = "ALREADY_EXISTS" 16 ErrorReason_PERMISSION_DENIED = "PERMISSION_DENIED" 17 ErrorReason_RESOURCE_EXHAUSTED = "RESOURCE_EXHAUSTED" 18 ErrorReason_FAILED_PRECONDITION = "FAILED_PRECONDITION" 19 ErrorReason_ABORTED = "ABORTED" 20 ErrorReason_OUT_OF_RANGE = "OUT_OF_RANGE" 21 ErrorReason_UNIMPLEMENTED = "UNIMPLEMENTED" 22 ErrorReason_INTERNAL = "INTERNAL" 23 ErrorReason_UNAVAILABLE = "UNAVAILABLE" 24 ErrorReason_DATA_LOSS = "DATA_LOSS" 25 ErrorReason_UNAUTHENTICATED = "UNAUTHENTICATED" 26 ) 27 28 func NewGrainErrorResponse(reason, message string) *GrainErrorResponse { 29 return &GrainErrorResponse{ 30 Reason: reason, 31 Message: message, 32 } 33 } 34 35 func NewGrainErrorResponsef(reason, format string, args ...interface{}) *GrainErrorResponse { 36 return &GrainErrorResponse{ 37 Reason: reason, 38 Message: fmt.Sprintf(format, args...), 39 } 40 } 41 42 func (m *GrainErrorResponse) Error() string { 43 return fmt.Sprintf("grain error response, reason: %s, message: %s, metadata: %v", m.Reason, m.Message, m.Metadata) 44 } 45 46 func (m *GrainErrorResponse) Is(err error) bool { 47 if e := new(GrainErrorResponse); errors.As(err, &e) { 48 return e.Reason == m.Reason 49 } 50 return false 51 } 52 53 func (m *GrainErrorResponse) Errorf(format string, args ...interface{}) error { 54 return NewGrainErrorResponse(m.Reason, fmt.Sprintf(format, args...)) 55 } 56 57 func (m *GrainErrorResponse) WithMetadata(metadata map[string]string) *GrainErrorResponse { 58 m.Metadata = metadata 59 return m 60 } 61 62 func Reason(err error) string { 63 if err == nil { 64 return ErrorReason_UNKNOWN 65 } 66 return FromError(err).Reason 67 } 68 69 func FromError(err error) *GrainErrorResponse { 70 if err == nil { 71 return nil 72 } 73 if e := new(GrainErrorResponse); errors.As(err, &e) { 74 return e 75 } 76 77 ret := NewGrainErrorResponse( 78 ErrorReason_UNKNOWN, 79 err.Error(), 80 ) 81 return ret 82 }