mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 09:28:54 +00:00
wrap errors even if an error message is not provided (#5828)
This commit is contained in:
parent
bc53208b81
commit
36284fecf0
2 changed files with 12 additions and 6 deletions
|
|
@ -121,8 +121,8 @@ func newError(ctx context.Context, msg string, cause error, data map[string]inte
|
|||
}
|
||||
|
||||
func wrapError(ctx context.Context, msg string, cause error, data map[string]interface{}) error {
|
||||
if msg == "" || cause == nil {
|
||||
return cause
|
||||
if cause == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
stack := newStack(2)
|
||||
|
|
|
|||
|
|
@ -138,8 +138,10 @@ func TestWrap(t *testing.T) {
|
|||
ctx, cleanup := setup()
|
||||
defer cleanup()
|
||||
cause := errors.New("cause")
|
||||
err := Wrap(ctx, cause)
|
||||
require.Equal(t, err, cause)
|
||||
err := Wrap(ctx, cause).(*FleetError)
|
||||
require.Equal(t, err.msg, "")
|
||||
require.NotEmpty(t, err.stack.List())
|
||||
require.NotNil(t, err.cause)
|
||||
})
|
||||
|
||||
t.Run("with nil error provided", func(t *testing.T) {
|
||||
|
|
@ -179,9 +181,13 @@ func TestWrapNewWithData(t *testing.T) {
|
|||
t.Run("without message provided", func(t *testing.T) {
|
||||
ctx, cleanup := setup()
|
||||
defer cleanup()
|
||||
data := map[string]interface{}{"foo": make(chan int)}
|
||||
cause := errors.New("cause")
|
||||
err := WrapWithData(ctx, cause, "", map[string]interface{}{"foo": "bar"})
|
||||
require.Equal(t, err, cause)
|
||||
err := WrapWithData(ctx, cause, "", data).(*FleetError)
|
||||
require.Equal(t, err.msg, "")
|
||||
require.NotEmpty(t, err.stack.List())
|
||||
require.NotNil(t, err.cause)
|
||||
assert.Regexp(t, regexp.MustCompile(`{"error": ".+"}`), string(err.data))
|
||||
})
|
||||
|
||||
t.Run("with nil error provided", func(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue