Skip to content

Commit dff4373

Browse files
committed
return PackError from the Send when message packing failed
1 parent 5fa7e3c commit dff4373

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

connection.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func (c *Connection) writeMessage(w io.Writer, message *iso8583.Message) error {
401401
// default message writer
402402
packed, err := message.Pack()
403403
if err != nil {
404-
return utils.NewSafeError(&PackError{err}, "packing message")
404+
return utils.NewSafeError(&PackError{err}, "failed to pack message")
405405
}
406406

407407
// create header
@@ -514,10 +514,18 @@ func (c *Connection) writeLoop() {
514514
if err != nil {
515515
c.handleError(fmt.Errorf("writing message: %w", err))
516516

517-
// if it's a pack error, we can continue to write messages
518517
var packErr *PackError
519518
if errors.As(err, &packErr) {
519+
// let caller know that his message was not not sent
520+
// because of pack error. We don't set all type of errors to errCh
521+
// as this case is handled by handleConnectionError(err)
522+
// which sends the same error to all pending requests, including
523+
// this one
524+
req.errCh <- err
525+
520526
err = nil
527+
528+
// we can continue to write other messages
521529
continue
522530
}
523531

connection_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,34 @@ func TestClient_Send(t *testing.T) {
205205
require.NoError(t, c.Close())
206206
})
207207

208+
t.Run("returns PackError when it fails to pack message", func(t *testing.T) {
209+
c, err := connection.New(server.Addr, testSpec, readMessageLength, writeMessageLength)
210+
require.NoError(t, err)
211+
defer c.Close()
212+
213+
err = c.Connect()
214+
require.NoError(t, err)
215+
216+
message := iso8583.NewMessage(testSpec)
217+
218+
// setting MTI to 1 digit will cause PackError as it should be
219+
// 4 digits
220+
message.MTI("1")
221+
222+
// we should set STAN as we check it before we pack the message
223+
err = message.Field(11, "123456")
224+
require.NoError(t, err)
225+
226+
// when we send the message
227+
_, err = c.Send(message)
228+
229+
// then Send should return PackError
230+
require.Error(t, err)
231+
232+
var packError *connection.PackError
233+
require.ErrorAs(t, err, &packError)
234+
})
235+
208236
t.Run("returns UnpackError with RawMessage when it fails to unpack message", func(t *testing.T) {
209237
// Given
210238
// connection with specification different from server

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ require (
1212
github.com/kr/text v0.2.0 // indirect
1313
github.com/pmezard/go-difflib v1.0.0 // indirect
1414
github.com/yerden/go-util v1.1.4 // indirect
15-
golang.org/x/text v0.8.0 // indirect
15+
golang.org/x/text v0.9.0 // indirect
1616
gopkg.in/yaml.v3 v3.0.1 // indirect
1717
)

go.sum

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
66
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
77
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
88
github.com/mediocregopher/radix.v2 v0.0.0-20181115013041-b67df6e626f9/go.mod h1:fLRUbhbSd5Px2yKUaGYYPltlyxi1guJz1vCmo1RQL50=
9-
github.com/moov-io/iso8583 v0.14.1 h1:kebG3hiWW41UvDMnNDbmgtDxjLqLOSxWCIITDTT3QMk=
10-
github.com/moov-io/iso8583 v0.14.1/go.mod h1:RXWrTyAXP1diIY0J6sF0Jf7YRd9Y6fMV3+UFgKUfK/I=
11-
github.com/moov-io/iso8583 v0.15.1 h1:mg2+B7rX7ESS6xoZip4g5eTBFb7AgK+H5H/HToUKFPw=
12-
github.com/moov-io/iso8583 v0.15.1/go.mod h1:RXWrTyAXP1diIY0J6sF0Jf7YRd9Y6fMV3+UFgKUfK/I=
13-
github.com/moov-io/iso8583 v0.15.2 h1:2CdYXA1I+hY7AaE2Ey0eTMmb3YuwL63uVsHDWNBfJFE=
14-
github.com/moov-io/iso8583 v0.15.2/go.mod h1:P/u3VHcgXKB88jFNMoBL4clWWkHNqWDe8C8DoyKwxJk=
159
github.com/moov-io/iso8583 v0.15.3 h1:4N36RMCxplHmF8mQPFych2RCaGgqKvbMyNp1v5Jrk4A=
1610
github.com/moov-io/iso8583 v0.15.3/go.mod h1:P/u3VHcgXKB88jFNMoBL4clWWkHNqWDe8C8DoyKwxJk=
1711
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -21,17 +15,13 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
2115
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
2216
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2317
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
24-
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
25-
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
2618
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
2719
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
2820
github.com/yerden/go-util v1.1.4 h1:jd8JyjLHzpEs1ZZQzDkfRgosDtXp/BtIAV1kpNjVTtw=
2921
github.com/yerden/go-util v1.1.4/go.mod h1:3HeLrvtkEeAv67ARostM9Yn0DcAVqgJ3uAiCuywEEXk=
3022
golang.org/x/sys v0.0.0-20190913121621-c3b328c6e5a7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
31-
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
32-
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
33-
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
34-
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
23+
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
24+
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
3525
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3626
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
3727
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)