Skip to content

Commit

Permalink
add pack/unpack errors (#249)
Browse files Browse the repository at this point in the history
* add pack/unpack errors

* update go workflow
  • Loading branch information
alovak authored Jul 20, 2023
1 parent 89e5ecb commit 1b1d36a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ jobs:
run: choco install -y make mingw

- name: Check
if: runner.os == 'Linux'
run: make check
env:
GOLANGCI_LINTERS: gosec

- name: Go Tests
if: runner.os != 'Linux'
run: go test ./... -count 1 -short

- name: Upload Code Coverage
if: runner.os == 'Linux'
Expand Down
28 changes: 28 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package iso8583

// UnpackError returns error with possibility to access RawMessage when
// connection failed to unpack message
type UnpackError struct {
Err error
RawMessage []byte
}

func (e *UnpackError) Error() string {
return e.Err.Error()
}

func (e *UnpackError) Unwrap() error {
return e.Err
}

type PackError struct {
Err error
}

func (e *PackError) Error() string {
return e.Err.Error()
}

func (e *PackError) Unwrap() error {
return e.Err
}
23 changes: 23 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,18 @@ func (m *Message) GetFields() map[int]field.Field {
return fields
}

// Pack returns the packed message or an error if the message is invalid
// error is of type *PackError
func (m *Message) Pack() ([]byte, error) {
data, err := m.pack()
if err != nil {
return nil, &PackError{Err: err}
}

return data, nil
}

func (m *Message) pack() ([]byte, error) {
packed := []byte{}
m.Bitmap().Reset()

Expand Down Expand Up @@ -160,7 +171,19 @@ func (m *Message) Pack() ([]byte, error) {
return packed, nil
}

// Unpack unpacks the message from the given byte slice or returns an error
// which is of type *UnpackError and contains the raw message
func (m *Message) Unpack(src []byte) error {
if err := m.unpack(src); err != nil {
return &UnpackError{
Err: err,
RawMessage: src,
}
}
return nil
}

func (m *Message) unpack(src []byte) error {
var off int

// reset fields that were set
Expand Down
18 changes: 18 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,12 +535,26 @@ func TestPackUnpack(t *testing.T) {
assert.Equal(t, "Another test text", data.F120.Value())
})

t.Run("Pack invalid message returns error of *PackError type", func(t *testing.T) {
message := NewMessage(spec)
message.MTI("1")

_, err := message.Pack()
require.Error(t, err)

var packErr *PackError
require.ErrorAs(t, err, &packErr)
})

t.Run("Unpack nil", func(t *testing.T) {
message := NewMessage(spec)

err := message.Unpack(nil)

require.Error(t, err)

var unpackError *UnpackError
require.ErrorAs(t, err, &unpackError)
})

t.Run("Unpack short mti", func(t *testing.T) {
Expand All @@ -551,6 +565,10 @@ func TestPackUnpack(t *testing.T) {
err := message.Unpack([]byte(rawMsg))

require.Error(t, err)

var unpackError *UnpackError
require.ErrorAs(t, err, &unpackError)
require.Equal(t, rawMsg, unpackError.RawMessage)
})

// this test should check that BCD fields are packed and
Expand Down

0 comments on commit 1b1d36a

Please sign in to comment.