diff --git a/xerror/xerror.go b/xerror/xerror.go index 856dc13..e961d1f 100644 --- a/xerror/xerror.go +++ b/xerror/xerror.go @@ -87,6 +87,10 @@ func (e *XError) Error() string { return msg } + if msg == "" { + return cause.Error() + } + msg = fmt.Sprintf("%s: %v", msg, cause.Error()) return msg diff --git a/xerror/xerror_test.go b/xerror/xerror_test.go index c14714e..37facff 100644 --- a/xerror/xerror_test.go +++ b/xerror/xerror_test.go @@ -115,3 +115,15 @@ func TestXError_Info(t *testing.T) { assert.Equal(cause, errInfo.Cause) assert.Equal("high", errInfo.Values["level"]) } + +func TestWrap_withoutMessage(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestWrap_withoutMessage") + + cause := errors.New("wrong password") + + assert.Equal("wrong password", Wrap(cause).Error()) + assert.Equal("wrong password", Wrap(Wrap(cause)).Error()) + assert.Equal("wrong password", New("").Wrap(cause).Error()) +}