forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return errors from cached raw store (#28)
* Return errors from cached raw store * gracefully ignore cache write errors in ReadProtobuf and WriteProtobuf * fix fixture * Wrapf the use of ErrExceedsLimit * nosec math/rand * add test with caching failures for protobuf store * log and add metrics to record failures not caused by ErrFailedToWriteCache
- Loading branch information
Showing
9 changed files
with
365 additions
and
20 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,113 @@ | ||
// Contains utilities to use to create and consume simple errors. | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// A generic error code type. | ||
type ErrorCode = string | ||
|
||
type err struct { | ||
code ErrorCode | ||
message string | ||
} | ||
|
||
func (e *err) Error() string { | ||
return fmt.Sprintf("[%v] %v", e.code, e.message) | ||
} | ||
|
||
func (e *err) Code() ErrorCode { | ||
return e.code | ||
} | ||
|
||
type errorWithCause struct { | ||
*err | ||
cause error | ||
} | ||
|
||
func (e *errorWithCause) Error() string { | ||
return fmt.Sprintf("%v, caused by: %v", e.err.Error(), errors.Cause(e)) | ||
} | ||
|
||
func (e *errorWithCause) Cause() error { | ||
return e.cause | ||
} | ||
|
||
// Creates a new error using an error code and a message. | ||
func Errorf(errorCode ErrorCode, msgFmt string, args ...interface{}) error { | ||
return &err{ | ||
code: errorCode, | ||
message: fmt.Sprintf(msgFmt, args...), | ||
} | ||
} | ||
|
||
// Wraps a root cause error with another. This is useful to unify an error type in a package. | ||
func Wrapf(code ErrorCode, cause error, msgFmt string, args ...interface{}) error { | ||
return &errorWithCause{ | ||
err: &err{ | ||
code: code, | ||
message: fmt.Sprintf(msgFmt, args...), | ||
}, | ||
cause: cause, | ||
} | ||
} | ||
|
||
// Gets the error code of the passed error if it has one. | ||
func GetErrorCode(e error) (code ErrorCode, found bool) { | ||
type coder interface { | ||
Code() ErrorCode | ||
} | ||
|
||
er, ok := e.(coder) | ||
if ok { | ||
return er.Code(), true | ||
} | ||
|
||
return | ||
} | ||
|
||
// Gets whether error is caused by another error with errCode. | ||
func IsCausedBy(e error, errCode ErrorCode) bool { | ||
type causer interface { | ||
Cause() error | ||
} | ||
|
||
for e != nil { | ||
if code, found := GetErrorCode(e); found && code == errCode { | ||
return true | ||
} | ||
|
||
cause, ok := e.(causer) | ||
if !ok { | ||
break | ||
} | ||
|
||
e = cause.Cause() | ||
} | ||
|
||
return false | ||
} | ||
|
||
func IsCausedByError(e, e2 error) bool { | ||
type causer interface { | ||
Cause() error | ||
} | ||
|
||
for e != nil { | ||
if e == e2 { | ||
return true | ||
} | ||
|
||
cause, ok := e.(causer) | ||
if !ok { | ||
break | ||
} | ||
|
||
e = cause.Cause() | ||
} | ||
|
||
return false | ||
} |
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,47 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestErrorf(t *testing.T) { | ||
e := Errorf("Code1", "msg") | ||
assert.NotNil(t, e) | ||
assert.Equal(t, "[Code1] msg", e.Error()) | ||
} | ||
|
||
func TestWrapf(t *testing.T) { | ||
e := Wrapf("Code1", fmt.Errorf("test error"), "msg") | ||
assert.NotNil(t, e) | ||
assert.Equal(t, "[Code1] msg, caused by: test error", e.Error()) | ||
} | ||
|
||
func TestGetErrorCode(t *testing.T) { | ||
e := Errorf("Code1", "msg") | ||
assert.NotNil(t, e) | ||
code, found := GetErrorCode(e) | ||
assert.True(t, found) | ||
assert.Equal(t, "Code1", code) | ||
} | ||
|
||
func TestIsCausedBy(t *testing.T) { | ||
e := Errorf("Code1", "msg") | ||
assert.NotNil(t, e) | ||
|
||
e = Wrapf("Code2", e, "msg") | ||
assert.True(t, IsCausedBy(e, "Code1")) | ||
assert.True(t, IsCausedBy(e, "Code2")) | ||
} | ||
|
||
func TestIsCausedByError(t *testing.T) { | ||
eRoot := Errorf("Code1", "msg") | ||
assert.NotNil(t, eRoot) | ||
e1 := Wrapf("Code2", eRoot, "msg") | ||
assert.True(t, IsCausedByError(e1, eRoot)) | ||
e2 := Wrapf("Code3", e1, "msg") | ||
assert.True(t, IsCausedByError(e2, eRoot)) | ||
assert.True(t, IsCausedByError(e2, e1)) | ||
} |
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
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
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.