From cd9ca5785f62152c1d793032f3f2fdbbdfe151bf Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 15 Jul 2024 16:52:50 +0100 Subject: [PATCH] Remove FieldIDs --- errors/errors.go | 28 +++++++++++++++++++++++----- field/composite.go | 10 ++-------- field/composite_test.go | 6 +++--- message.go | 6 ------ message_test.go | 18 +++++++++--------- 5 files changed, 37 insertions(+), 31 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index 15840fc..ec2f906 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -1,13 +1,14 @@ package errors +import ( + "errors" +) + // UnpackError returns an error with the possibility to access the RawMessage when // the connection failed to unpack the message type UnpackError struct { - Err error - // the field ID of the field on which unpacking errored - FieldID string - // the field ID and subfield IDs (if any) that errored ordered from outermost inwards - FieldIDs []string + Err error + FieldID string RawMessage []byte } @@ -19,6 +20,23 @@ func (e *UnpackError) Unwrap() error { return e.Err } +// FieldIDs returns the list of field and subfield IDs (if any) that errored from outermost inwards +func (e *UnpackError) FieldIDs() []string { + fieldIDs := []string{e.FieldID} + err := e.Err + var unpackError *UnpackError + for { + if errors.As(err, &unpackError) { + fieldIDs = append(fieldIDs, unpackError.FieldID) + err = unpackError.Unwrap() + } else { + break + } + } + + return fieldIDs +} + type PackError struct { Err error } diff --git a/field/composite.go b/field/composite.go index 850b101..b119b12 100644 --- a/field/composite.go +++ b/field/composite.go @@ -10,7 +10,7 @@ import ( "sync" "github.com/moov-io/iso8583/encoding" - mooverrors "github.com/moov-io/iso8583/errors" + iso8583errors "github.com/moov-io/iso8583/errors" "github.com/moov-io/iso8583/prefix" "github.com/moov-io/iso8583/sort" "github.com/moov-io/iso8583/utils" @@ -522,15 +522,9 @@ func (f *Composite) packByTag() ([]byte, error) { func (f *Composite) wrapErrorUnpack(src []byte, isVariableLength bool) (int, error) { offset, tagID, err := f.unpack(src, isVariableLength) if err != nil { - subfields := []string{} - var unpackErr *mooverrors.UnpackError - if errors.As(err, &unpackErr) { - subfields = unpackErr.FieldIDs - } - return offset, &mooverrors.UnpackError{ + return offset, &iso8583errors.UnpackError{ Err: err, FieldID: tagID, - FieldIDs: append([]string{tagID}, subfields...), RawMessage: src, } } diff --git a/field/composite_test.go b/field/composite_test.go index 53dc52e..7651b88 100644 --- a/field/composite_test.go +++ b/field/composite_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/moov-io/iso8583/encoding" - mooverrors "github.com/moov-io/iso8583/errors" + iso8583errors "github.com/moov-io/iso8583/errors" "github.com/moov-io/iso8583/padding" "github.com/moov-io/iso8583/prefix" "github.com/moov-io/iso8583/sort" @@ -801,10 +801,10 @@ func TestCompositePacking(t *testing.T) { read, err := composite.Unpack([]byte("ABCDEF")) require.Equal(t, 0, read) require.Error(t, err) - var unpackError *mooverrors.UnpackError + var unpackError *iso8583errors.UnpackError require.ErrorAs(t, err, &unpackError) assert.Equal(t, "3", unpackError.FieldID) - assert.Equal(t, []string{"3"}, unpackError.FieldIDs) + assert.Equal(t, []string{"3"}, unpackError.FieldIDs()) require.EqualError(t, err, "failed to unpack subfield 3: failed to set bytes: failed to convert into number") require.ErrorIs(t, err, strconv.ErrSyntax) }) diff --git a/message.go b/message.go index aca03ea..1a6640a 100644 --- a/message.go +++ b/message.go @@ -239,15 +239,9 @@ func (m *Message) Unpack(src []byte) error { // locked by the caller. func (m *Message) wrapErrorUnpack(src []byte) error { if fieldID, err := m.unpack(src); err != nil { - subfields := []string{} - var unpackErr *iso8583errors.UnpackError - if errors.As(err, &unpackErr) { - subfields = unpackErr.FieldIDs - } return &iso8583errors.UnpackError{ Err: err, FieldID: fieldID, - FieldIDs: append([]string{fieldID}, subfields...), RawMessage: src, } } diff --git a/message_test.go b/message_test.go index 78ccc75..09fd185 100644 --- a/message_test.go +++ b/message_test.go @@ -10,7 +10,7 @@ import ( "time" "github.com/moov-io/iso8583/encoding" - mooverrors "github.com/moov-io/iso8583/errors" + iso8583errors "github.com/moov-io/iso8583/errors" "github.com/moov-io/iso8583/field" "github.com/moov-io/iso8583/padding" "github.com/moov-io/iso8583/prefix" @@ -723,7 +723,7 @@ func TestPackUnpack(t *testing.T) { _, err := message.Pack() require.Error(t, err) - var packErr *mooverrors.PackError + var packErr *iso8583errors.PackError require.ErrorAs(t, err, &packErr) }) @@ -734,7 +734,7 @@ func TestPackUnpack(t *testing.T) { require.Error(t, err) - var unpackError *mooverrors.UnpackError + var unpackError *iso8583errors.UnpackError require.ErrorAs(t, err, &unpackError) }) @@ -747,7 +747,7 @@ func TestPackUnpack(t *testing.T) { require.Error(t, err) - var unpackError *mooverrors.UnpackError + var unpackError *iso8583errors.UnpackError require.ErrorAs(t, err, &unpackError) require.Equal(t, rawMsg, unpackError.RawMessage) }) @@ -761,7 +761,7 @@ func TestPackUnpack(t *testing.T) { err := message.Unpack([]byte(rawMsg)) require.Error(t, err) - var unpackError *mooverrors.UnpackError + var unpackError *iso8583errors.UnpackError require.ErrorAs(t, err, &unpackError) assert.Equal(t, unpackError.FieldID, "120") @@ -884,10 +884,10 @@ func TestPackUnpack(t *testing.T) { err := message.Unpack([]byte(rawMsg)) require.Error(t, err) - var unpackError *mooverrors.UnpackError + var unpackError *iso8583errors.UnpackError require.ErrorAs(t, err, &unpackError) assert.Equal(t, "3", unpackError.FieldID) - assert.Equal(t, []string{"3", "2"}, unpackError.FieldIDs) + assert.Equal(t, []string{"3", "2"}, unpackError.FieldIDs()) s, err := message.GetString(2) require.NoError(t, err) @@ -1003,10 +1003,10 @@ func TestPackUnpack(t *testing.T) { err := message.Unpack([]byte(rawMsg)) require.Error(t, err) - var unpackError *mooverrors.UnpackError + var unpackError *iso8583errors.UnpackError require.ErrorAs(t, err, &unpackError) assert.Equal(t, "3", unpackError.FieldID) - assert.Equal(t, []string{"3", "2", "2"}, unpackError.FieldIDs) + assert.Equal(t, []string{"3", "2", "2"}, unpackError.FieldIDs()) s, err := message.GetString(2) require.NoError(t, err)