Skip to content

Commit

Permalink
Remove FieldIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
meparle committed Jul 15, 2024
1 parent 41e97e5 commit cd9ca57
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
28 changes: 23 additions & 5 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -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
}
Expand Down
10 changes: 2 additions & 8 deletions field/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}
}
Expand Down
6 changes: 3 additions & 3 deletions field/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
Expand Down
6 changes: 0 additions & 6 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
18 changes: 9 additions & 9 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})

Expand All @@ -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)
})

Expand All @@ -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)
})
Expand All @@ -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")

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cd9ca57

Please sign in to comment.