From a55ffa47966a27b469ee78e70a763674c9e5843e Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 16 Mar 2022 16:54:20 +0300 Subject: [PATCH] [#176] status: Do not lose built-in error text message Signed-off-by: Alex Vanin --- client/status/v2.go | 8 ++++++-- client/status/v2_test.go | 26 ++++++++++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/client/status/v2.go b/client/status/v2.go index 2890f8c..5c2e2be 100644 --- a/client/status/v2.go +++ b/client/status/v2.go @@ -97,7 +97,8 @@ func FromStatusV2(st *status.Status) Status { // ToStatusV2 converts Status instance to status.Status message structure. Inverse to FromStatusV2 operation. // // If argument is the StatusV2 instance, it is converted directly. -// Otherwise, successes are converted with status.OK code w/o details and message, failures - with status.Internal. +// Otherwise, successes are converted with status.OK code w/o details and message, +// failures - with status.Internal and error text message w/o details. func ToStatusV2(st Status) *status.Status { if v, ok := st.(StatusV2); ok { return v.ToStatusV2() @@ -107,7 +108,10 @@ func ToStatusV2(st Status) *status.Status { return newStatusV2WithLocalCode(status.OK, status.GlobalizeSuccess) } - return newStatusV2WithLocalCode(status.Internal, status.GlobalizeCommonFail) + internalErrorStatus := newStatusV2WithLocalCode(status.Internal, status.GlobalizeCommonFail) + internalErrorStatus.SetMessage(st.(error).Error()) // type cast never panics because IsSuccessful() checks cast + + return internalErrorStatus } func errMessageStatusV2(code interface{}, msg string) string { diff --git a/client/status/v2_test.go b/client/status/v2_test.go index b80486c..de27095 100644 --- a/client/status/v2_test.go +++ b/client/status/v2_test.go @@ -12,12 +12,14 @@ func TestToStatusV2(t *testing.T) { type statusConstructor func() apistatus.Status for _, testItem := range [...]struct { - status interface{} // Status or statusConstructor - codeV2 uint64 + status interface{} // Status or statusConstructor + codeV2 uint64 + messageV2 string }{ { - status: errors.New("some error"), - codeV2: 1024, + status: errors.New("some error"), + codeV2: 1024, + messageV2: "some error", }, { status: 1, @@ -124,6 +126,9 @@ func TestToStatusV2(t *testing.T) { // must generate the same status.Status message require.EqualValues(t, testItem.codeV2, stv2.Code()) + if len(testItem.messageV2) > 0 { + require.Equal(t, testItem.messageV2, stv2.Message()) + } _, ok := st.(apistatus.StatusV2) if ok { @@ -142,12 +147,14 @@ func TestFromStatusV2(t *testing.T) { type statusConstructor func() apistatus.Status for _, testItem := range [...]struct { - status interface{} // Status or statusConstructor - codeV2 uint64 + status interface{} // Status or statusConstructor + codeV2 uint64 + messageV2 string }{ { - status: errors.New("some error"), - codeV2: 1024, + status: errors.New("some error"), + codeV2: 1024, + messageV2: "some error", }, { status: 1, @@ -254,6 +261,9 @@ func TestFromStatusV2(t *testing.T) { // must generate the same status.Status message require.EqualValues(t, testItem.codeV2, stv2.Code()) + if len(testItem.messageV2) > 0 { + require.Equal(t, testItem.messageV2, stv2.Message()) + } _, ok := st.(apistatus.StatusV2) if ok {