From e4628881e4e3af1d3d15e305664e7eddf0d99d1e Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Mon, 28 Aug 2023 20:30:54 +0300 Subject: [PATCH 1/5] Use last error holder instead of the first --- internal/hzerrors/errors.go | 28 +++++++++++++--------------- internal/proto/codec/builtin.go | 3 +-- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/internal/hzerrors/errors.go b/internal/hzerrors/errors.go index c84ac4318..a6a03a6dd 100644 --- a/internal/hzerrors/errors.go +++ b/internal/hzerrors/errors.go @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved. + * Copyright (c) 2008-2023, Hazelcast, Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License") * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import ( "strings" "github.com/hazelcast/hazelcast-go-client/hzerrors" + "github.com/hazelcast/hazelcast-go-client/internal/proto" ) // StackTraceElement contains stacktrace information for server side exception. @@ -39,33 +40,30 @@ type StackTraceElement struct { } type ServerError struct { - ClassName string - Message string - CauseClassName string - StackTrace []StackTraceElement - ErrorCode int32 - CauseErrorCode int32 + errorHolders []proto.ErrorHolder } -func NewServerError(errorCode int32, className string, message string, stackTrace []StackTraceElement) *ServerError { +func NewServerError(errorHolders []proto.ErrorHolder) *ServerError { return &ServerError{ - ErrorCode: errorCode, - ClassName: className, - Message: message, - StackTrace: stackTrace, + errorHolders: errorHolders, } } func (e *ServerError) Error() string { - return e.Message + return e.lastErrorHolder().Message } func (e *ServerError) String() string { sb := strings.Builder{} - for _, trace := range e.StackTrace { + eh := e.lastErrorHolder() + for _, trace := range eh.StackTraceElements { sb.WriteString(fmt.Sprintf("\n %s.%s(%s:%d)", trace.ClassName, trace.MethodName, trace.FileName, trace.LineNumber)) } - return fmt.Sprintf("got exception from server:\n %s: %s\n %s", e.ClassName, e.Message, sb.String()) + return fmt.Sprintf("got exception from server:\n %s: %s\n %s", eh.ClassName, eh.Message, sb.String()) +} + +func (e *ServerError) lastErrorHolder() proto.ErrorHolder { + return e.errorHolders[len(e.errorHolders)-1] } type ClientError struct { diff --git a/internal/proto/codec/builtin.go b/internal/proto/codec/builtin.go index 329565ee8..b7ce42208 100644 --- a/internal/proto/codec/builtin.go +++ b/internal/proto/codec/builtin.go @@ -792,8 +792,7 @@ func DecodeError(msg *proto.ClientMessage) *ihzerrors.ServerError { if len(errorHolders) == 0 { return nil } - holder := errorHolders[0] - return ihzerrors.NewServerError(holder.ErrorCode, holder.ClassName, holder.Message, holder.StackTraceElements) + return ihzerrors.NewServerError(errorHolders) } func NewEndpointQualifier(qualifierType int32, identifier string) pubcluster.EndpointQualifier { From 9ed042e7f4d968f5a53c0483a988872107007fb3 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Mon, 28 Aug 2023 20:39:47 +0300 Subject: [PATCH 2/5] Moved ErrorHolder to ihzerrors --- internal/cluster/connection.go | 2 +- internal/hzerrors/errors.go | 15 ++++++---- internal/proto/codec/builtin.go | 2 +- internal/proto/codec/error_holder_codec.go | 9 +++--- internal/proto/error_holder.go | 35 ---------------------- 5 files changed, 16 insertions(+), 47 deletions(-) delete mode 100644 internal/proto/error_holder.go diff --git a/internal/cluster/connection.go b/internal/cluster/connection.go index 6d989b862..a3102cadb 100644 --- a/internal/cluster/connection.go +++ b/internal/cluster/connection.go @@ -337,7 +337,7 @@ func positiveDurationOrMax(duration time.Duration) time.Duration { } func wrapError(err *ihzerrors.ServerError) error { - targetErr := convertErrorCodeToError(errorCode(err.ErrorCode)) + targetErr := convertErrorCodeToError(errorCode(err.ErrorCode())) return ihzerrors.NewClientError(err.String(), err, targetErr) } diff --git a/internal/hzerrors/errors.go b/internal/hzerrors/errors.go index a6a03a6dd..3c414911d 100644 --- a/internal/hzerrors/errors.go +++ b/internal/hzerrors/errors.go @@ -22,7 +22,6 @@ import ( "strings" "github.com/hazelcast/hazelcast-go-client/hzerrors" - "github.com/hazelcast/hazelcast-go-client/internal/proto" ) // StackTraceElement contains stacktrace information for server side exception. @@ -40,20 +39,24 @@ type StackTraceElement struct { } type ServerError struct { - errorHolders []proto.ErrorHolder + errorHolders []ErrorHolder } -func NewServerError(errorHolders []proto.ErrorHolder) *ServerError { +func NewServerError(errorHolders []ErrorHolder) *ServerError { return &ServerError{ errorHolders: errorHolders, } } -func (e *ServerError) Error() string { +func (e ServerError) ErrorCode() int32 { + return e.errorHolders[0].ErrorCode +} + +func (e ServerError) Error() string { return e.lastErrorHolder().Message } -func (e *ServerError) String() string { +func (e ServerError) String() string { sb := strings.Builder{} eh := e.lastErrorHolder() for _, trace := range eh.StackTraceElements { @@ -62,7 +65,7 @@ func (e *ServerError) String() string { return fmt.Sprintf("got exception from server:\n %s: %s\n %s", eh.ClassName, eh.Message, sb.String()) } -func (e *ServerError) lastErrorHolder() proto.ErrorHolder { +func (e ServerError) lastErrorHolder() ErrorHolder { return e.errorHolders[len(e.errorHolders)-1] } diff --git a/internal/proto/codec/builtin.go b/internal/proto/codec/builtin.go index b7ce42208..c4546f021 100644 --- a/internal/proto/codec/builtin.go +++ b/internal/proto/codec/builtin.go @@ -785,7 +785,7 @@ func DecodeString(frameIterator *proto.ForwardFrameIterator) string { func DecodeError(msg *proto.ClientMessage) *ihzerrors.ServerError { frameIterator := msg.FrameIterator() frameIterator.Next() - var errorHolders []proto.ErrorHolder + var errorHolders []ihzerrors.ErrorHolder DecodeListMultiFrame(frameIterator, func(it *proto.ForwardFrameIterator) { errorHolders = append(errorHolders, DecodeErrorHolder(frameIterator)) }) diff --git a/internal/proto/codec/error_holder_codec.go b/internal/proto/codec/error_holder_codec.go index 38236b628..a5985c890 100644 --- a/internal/proto/codec/error_holder_codec.go +++ b/internal/proto/codec/error_holder_codec.go @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved. + * Copyright (c) 2008-2023, Hazelcast, Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License") * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package codec import ( + ihzerrors "github.com/hazelcast/hazelcast-go-client/internal/hzerrors" "github.com/hazelcast/hazelcast-go-client/internal/proto" ) @@ -24,7 +25,7 @@ const ( ErrorHolderCodecErrorCodeInitialFrameSize = ErrorHolderCodecErrorCodeFieldOffset + proto.IntSizeInBytes ) -func EncodeErrorHolder(clientMessage *proto.ClientMessage, errorHolder proto.ErrorHolder) { +func EncodeErrorHolder(clientMessage *proto.ClientMessage, errorHolder ihzerrors.ErrorHolder) { clientMessage.AddFrame(proto.BeginFrame.Copy()) initialFrame := proto.NewFrame(make([]byte, ErrorHolderCodecErrorCodeInitialFrameSize)) FixSizedTypesCodec.EncodeInt(initialFrame.Content, ErrorHolderCodecErrorCodeFieldOffset, int32(errorHolder.ErrorCode)) @@ -37,7 +38,7 @@ func EncodeErrorHolder(clientMessage *proto.ClientMessage, errorHolder proto.Err clientMessage.AddFrame(proto.EndFrame.Copy()) } -func DecodeErrorHolder(frameIterator *proto.ForwardFrameIterator) proto.ErrorHolder { +func DecodeErrorHolder(frameIterator *proto.ForwardFrameIterator) ihzerrors.ErrorHolder { // begin frame frameIterator.Next() initialFrame := frameIterator.Next() @@ -47,5 +48,5 @@ func DecodeErrorHolder(frameIterator *proto.ForwardFrameIterator) proto.ErrorHol message := CodecUtil.DecodeNullableForString(frameIterator) stackTraceElements := DecodeListMultiFrameForStackTraceElement(frameIterator) CodecUtil.FastForwardToEndFrame(frameIterator) - return proto.NewErrorHolder(errorCode, className, message, stackTraceElements) + return ihzerrors.NewErrorHolder(errorCode, className, message, stackTraceElements) } diff --git a/internal/proto/error_holder.go b/internal/proto/error_holder.go deleted file mode 100644 index 6f5733731..000000000 --- a/internal/proto/error_holder.go +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License") - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package proto - -import "github.com/hazelcast/hazelcast-go-client/internal/hzerrors" - -type ErrorHolder struct { - ClassName string - Message string - StackTraceElements []hzerrors.StackTraceElement - ErrorCode int32 -} - -func NewErrorHolder(errorCode int32, className, message string, stackTraceElements []hzerrors.StackTraceElement) ErrorHolder { - return ErrorHolder{ - ErrorCode: errorCode, - ClassName: className, - Message: message, - StackTraceElements: stackTraceElements, - } -} From f9a31aa3afad37efdb91796e800e0daa9c5a1212 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Mon, 28 Aug 2023 20:45:20 +0300 Subject: [PATCH 3/5] Added missing error_holder.go --- internal/hzerrors/error_holder.go | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 internal/hzerrors/error_holder.go diff --git a/internal/hzerrors/error_holder.go b/internal/hzerrors/error_holder.go new file mode 100644 index 000000000..07dd87349 --- /dev/null +++ b/internal/hzerrors/error_holder.go @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2008-2023, Hazelcast, Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License") + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package hzerrors + +type ErrorHolder struct { + ClassName string + Message string + StackTraceElements []StackTraceElement + ErrorCode int32 +} + +func NewErrorHolder(errorCode int32, className, message string, stackTraceElements []StackTraceElement) ErrorHolder { + return ErrorHolder{ + ErrorCode: errorCode, + ClassName: className, + Message: message, + StackTraceElements: stackTraceElements, + } +} From 53c43fbe5a4c01a402d5d5712eb170873897b15a Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Fri, 8 Sep 2023 12:34:51 +0300 Subject: [PATCH 4/5] Updated server error format --- internal/hzerrors/errors.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/hzerrors/errors.go b/internal/hzerrors/errors.go index 3c414911d..c95926728 100644 --- a/internal/hzerrors/errors.go +++ b/internal/hzerrors/errors.go @@ -53,16 +53,18 @@ func (e ServerError) ErrorCode() int32 { } func (e ServerError) Error() string { - return e.lastErrorHolder().Message + return fmt.Sprintf("server error: %s: %s", e.lastErrorHolder().ClassName, e.lastErrorHolder().Message) } func (e ServerError) String() string { - sb := strings.Builder{} - eh := e.lastErrorHolder() - for _, trace := range eh.StackTraceElements { - sb.WriteString(fmt.Sprintf("\n %s.%s(%s:%d)", trace.ClassName, trace.MethodName, trace.FileName, trace.LineNumber)) + var sb strings.Builder + for _, h := range e.errorHolders { + sb.WriteString(fmt.Sprintf("%s: %s\n")) + for _, trace := range h.StackTraceElements { + sb.WriteString(fmt.Sprintf("\t%s.%s(%s:%d)", trace.ClassName, trace.MethodName, trace.FileName, trace.LineNumber)) + } } - return fmt.Sprintf("got exception from server:\n %s: %s\n %s", eh.ClassName, eh.Message, sb.String()) + return fmt.Sprintf("server error:\n%s", sb.String()) } func (e ServerError) lastErrorHolder() ErrorHolder { From 3c2997917828ecec680aa85926c2a57833e8dc07 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Fri, 8 Sep 2023 12:39:50 +0300 Subject: [PATCH 5/5] Updated server error format --- internal/hzerrors/errors.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/hzerrors/errors.go b/internal/hzerrors/errors.go index c95926728..971a48631 100644 --- a/internal/hzerrors/errors.go +++ b/internal/hzerrors/errors.go @@ -59,9 +59,9 @@ func (e ServerError) Error() string { func (e ServerError) String() string { var sb strings.Builder for _, h := range e.errorHolders { - sb.WriteString(fmt.Sprintf("%s: %s\n")) + sb.WriteString(fmt.Sprintf("%s: %s\n", h.ClassName, h.Message)) for _, trace := range h.StackTraceElements { - sb.WriteString(fmt.Sprintf("\t%s.%s(%s:%d)", trace.ClassName, trace.MethodName, trace.FileName, trace.LineNumber)) + sb.WriteString(fmt.Sprintf("\t%s.%s(%s:%d)\n", trace.ClassName, trace.MethodName, trace.FileName, trace.LineNumber)) } } return fmt.Sprintf("server error:\n%s", sb.String())