Skip to content

Commit

Permalink
use (Un)PackError from iso8583 package instead of using own types (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
alovak authored Jul 21, 2023
1 parent 8970c5b commit 28e94e2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 40 deletions.
41 changes: 5 additions & 36 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,6 @@ const (
StatusUnknown Status = ""
)

// 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
}

// Connection represents an ISO 8583 Connection. Connection may be used
// by multiple goroutines simultaneously.
type Connection struct {
Expand Down Expand Up @@ -413,7 +386,7 @@ func (c *Connection) writeMessage(w io.Writer, message *iso8583.Message) error {
// default message writer
packed, err := message.Pack()
if err != nil {
return utils.NewSafeError(&PackError{err}, "failed to pack message")
return fmt.Errorf("packing message: %w", err)
}

// create buffer for header and packed message so we can write it to
Expand Down Expand Up @@ -536,7 +509,7 @@ func (c *Connection) writeLoop() {
if err != nil {
c.handleError(fmt.Errorf("writing message: %w", err))

var packErr *PackError
var packErr *iso8583.PackError
if errors.As(err, &packErr) {
// let caller know that the message was not sent because of pack error.
// We don't set all type of errors to errCh as this case is handled
Expand Down Expand Up @@ -587,9 +560,9 @@ func (c *Connection) readLoop() {
if err != nil {
c.handleError(utils.NewSafeError(err, "failed to read message from connection"))

// if err is ErrUnpack, we can still continue reading
// if err is UnpackError, we can still continue reading
// from the connection
var unpackErr *UnpackError
var unpackErr *iso8583.UnpackError
if errors.As(err, &unpackErr) {
continue
}
Expand Down Expand Up @@ -635,11 +608,7 @@ func (c *Connection) readMessage(r io.Reader) (*iso8583.Message, error) {
message := iso8583.NewMessage(c.spec)
err = message.Unpack(rawMessage)
if err != nil {
unpackErr := &UnpackError{
Err: err,
RawMessage: rawMessage,
}
return nil, fmt.Errorf("unpacking message: %w", unpackErr)
return nil, fmt.Errorf("unpacking message: %w", err)
}

return message, nil
Expand Down
4 changes: 2 additions & 2 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func TestClient_Send(t *testing.T) {
// then Send should return PackError
require.Error(t, err)

var packError *connection.PackError
var packError *iso8583.PackError
require.ErrorAs(t, err, &packError)
})

Expand Down Expand Up @@ -367,7 +367,7 @@ func TestClient_Send(t *testing.T) {
mu.Lock()
defer mu.Unlock()

var unpackErr *connection.UnpackError
var unpackErr *iso8583.UnpackError
if errors.As(handledError, &unpackErr) {
require.EqualError(t, handledError, "failed to read message from connection")
require.EqualError(t, unpackErr, "failed to unpack field 63: no specification found")
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/moov-io/iso8583-connection
go 1.19

require (
github.com/moov-io/iso8583 v0.18.2
github.com/moov-io/iso8583 v0.18.3
github.com/stretchr/testify v1.8.4
)

Expand All @@ -12,6 +12,6 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/yerden/go-util v1.1.4 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ github.com/moov-io/iso8583 v0.18.1 h1:J0amw2rhoTv2l5nvY7n4Ee4A34SSBrZESfrZ+Dvke0
github.com/moov-io/iso8583 v0.18.1/go.mod h1:rqnoLk9z66a2S6OAo6oeVJsGrGwr/MQkx2G7frxS5RI=
github.com/moov-io/iso8583 v0.18.2 h1:7D/zGxhV7YK23V1SfARU/JcJFVAW9jp0j1k1W5z4WyY=
github.com/moov-io/iso8583 v0.18.2/go.mod h1:yq1HVUdtNG+NXY0YIaD4od/QOKCCOSk6/ygU2aTE/E0=
github.com/moov-io/iso8583 v0.18.3-0.20230720140054-1b1d36aa2970 h1:PdUPxYek+MeOea1GfjATYLSDZolsS6IDoUbAsIq6Its=
github.com/moov-io/iso8583 v0.18.3-0.20230720140054-1b1d36aa2970/go.mod h1:QPxDQTxKJPjECpK8vCsh7TpwvKyIrw3tXN/lrdATzCA=
github.com/moov-io/iso8583 v0.18.3 h1:RYr/zoNxeRcLpJ1VjJ8e8yC0fRK5+xFqKqpnyMTz3+A=
github.com/moov-io/iso8583 v0.18.3/go.mod h1:QPxDQTxKJPjECpK8vCsh7TpwvKyIrw3tXN/lrdATzCA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand All @@ -20,6 +24,8 @@ golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down

0 comments on commit 28e94e2

Please sign in to comment.