From 0eabb67ed48d37bf73fbf92da5d0359000e01643 Mon Sep 17 00:00:00 2001 From: Lee Sang Gyu Date: Mon, 9 Nov 2026 13:27:37 +0900 Subject: [PATCH] fix(xerror): [xerror] drop leading ": " when message is empty (#423) * fix(xerror): [xerror] drop leading ": " when message is empty * test(xerror): [xerror] cover Wrap without a message --- xerror/xerror.go | 4 ++++ xerror/xerror_test.go | 12 ++++++++++++ 2 files changed, 16 insertions(+) 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()) +}