Skip to content

Commit

Permalink
feat: pass through more information on length mismatch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jun 6, 2023
1 parent 658dbe1 commit df51ffb
Show file tree
Hide file tree
Showing 57 changed files with 121 additions and 122 deletions.
4 changes: 2 additions & 2 deletions accountCreditedDrawdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (creditDD *AccountCreditedDrawdown) Parse(record string) error {
creditDD.DrawdownCreditAccountNumber = value
length += read

if !creditDD.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := creditDD.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions accountDebitedDrawdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (debitDD *AccountDebitedDrawdown) Parse(record string) error {
debitDD.Address.AddressLineThree = value
length += read

if !debitDD.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := debitDD.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions actualAmountPaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (aap *ActualAmountPaid) Parse(record string) error {
aap.RemittanceAmount.Amount = value
length += read

if !aap.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := aap.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions adjustment.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (adj *Adjustment) Parse(record string) error {
adj.AdditionalInfo = value
length += read

if !adj.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := adj.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions amountNegotiatedDiscount.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (nd *AmountNegotiatedDiscount) Parse(record string) error {
nd.RemittanceAmount.Amount = value
length += read

if !nd.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := nd.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions beneficiary.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (ben *Beneficiary) Parse(record string) error {
ben.Personal.Address.AddressLineThree = value
length += read

if !ben.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := ben.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions beneficiaryCustomer.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (bc *BeneficiaryCustomer) Parse(record string) error {
bc.CoverPayment.SwiftLineFive = value
length += read

if !bc.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := bc.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions beneficiaryFI.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (bfi *BeneficiaryFI) Parse(record string) error {
bfi.FinancialInstitution.Address.AddressLineThree = value
length += read

if !bfi.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := bfi.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions beneficiaryIntermediaryFI.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (bifi *BeneficiaryIntermediaryFI) Parse(record string) error {
bifi.FinancialInstitution.Address.AddressLineThree = value
length += read

if !bifi.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := bifi.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions beneficiaryReference.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (br *BeneficiaryReference) Parse(record string) error {
br.BeneficiaryReference = value
length += read

if !br.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := br.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions businessFunctionCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (bfc *BusinessFunctionCode) Parse(record string) error {
bfc.TransactionTypeCode = value
length += read

if !bfc.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := bfc.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions charges.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (c *Charges) Parse(record string) error {
c.SendersChargesFour = value
length += read

if !c.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := c.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
19 changes: 9 additions & 10 deletions converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package wire

import (
"fmt"
"strconv"
"strings"
"unicode/utf8"
)

// converters handles golang to WIRE type Converters
Expand Down Expand Up @@ -68,7 +70,6 @@ func (c *converters) formatAlphaField(s string, max uint, options FormatOptions)
}

func (c *converters) parseVariableStringField(r string, maxLen int) (got string, size int, err error) {

min := func(x, y int) int {
if x > y {
return y
Expand Down Expand Up @@ -151,15 +152,13 @@ func (c *converters) stripDelimiters(data string) string {
}

// verify input data with read length
func (c *converters) verifyDataWithReadLength(data string, read int) bool {
if len(data) == read {
return true
func (c *converters) verifyDataWithReadLength(data string, expected int) error {
n := utf8.RuneCountInString(data)
if n == expected {
return nil
}

// TODO: workaround for special case, not specification
if len(data) > read && data[read:] == "*" {
return true
if n > expected && data[expected:] == "*" {
return nil
}

return false
return fmt.Errorf("found data of %d length but expected %d", n, expected)
}
4 changes: 2 additions & 2 deletions currencyInstructedAmount.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func (cia *CurrencyInstructedAmount) Parse(record string) error {
cia.Amount = cia.parseStringField(record[length : length+18])
length += 18

if !cia.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := cia.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions errorWire.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func (ew *ErrorWire) Parse(record string) error {
ew.ErrorDescription = value
length += read

if !ew.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := ew.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions exchangeRate.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func (eRate *ExchangeRate) Parse(record string) error {
eRate.ExchangeRate = value
length += read

if !eRate.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := eRate.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions fIBeneficiaryFIAdvice.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (fibfia *FIBeneficiaryFIAdvice) Parse(record string) error {
fibfia.Advice.LineSix = value
length += read

if !fibfia.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := fibfia.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions fiAdditionalFIToFI.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (fifi *FIAdditionalFIToFI) Parse(record string) error {
fifi.AdditionalFIToFI.LineSix = value
length += read

if !fifi.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := fifi.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions fiBeneficiary.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (fib *FIBeneficiary) Parse(record string) error {
fib.FIToFI.LineSix = value
length += read

if !fib.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := fib.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions fiBeneficiaryAdvice.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (fiba *FIBeneficiaryAdvice) Parse(record string) error {
fiba.Advice.LineSix = value
length += read

if !fiba.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := fiba.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions fiBeneficiaryFI.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (fibfi *FIBeneficiaryFI) Parse(record string) error {
fibfi.FIToFI.LineSix = value
length += read

if !fibfi.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := fibfi.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions fiDrawdownDebitAccountAdvice.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (debitDDAdvice *FIDrawdownDebitAccountAdvice) Parse(record string) error {
debitDDAdvice.Advice.LineSix = value
length += read

if !debitDDAdvice.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := debitDDAdvice.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions fiIntermediaryFI.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (fiifi *FIIntermediaryFI) Parse(record string) error {
fiifi.FIToFI.LineSix = value
length += read

if !fiifi.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := fiifi.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions fiIntermediaryFIAdvice.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (fiifia *FIIntermediaryFIAdvice) Parse(record string) error {
fiifia.Advice.LineSix = value
length += read

if !fiifia.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := fiifia.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions fiPaymentMethodToBeneficiary.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (pm *FIPaymentMethodToBeneficiary) Parse(record string) error {
pm.AdditionalInformation = value
length += read

if !pm.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := pm.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions fiReceiverFI.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (firfi *FIReceiverFI) Parse(record string) error {
firfi.FIToFI.LineSix = value
length += read

if !firfi.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := firfi.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions fileErrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func NewTagMinLengthErr(tagLength, length int) TagWrongLengthErr {
}

// NewTagMaxLengthErr creates a new error of the TagWrongLengthErr type
func NewTagMaxLengthErr() TagWrongLengthErr {
func NewTagMaxLengthErr(err error) TagWrongLengthErr {
return TagWrongLengthErr{
Message: "contains invalid information in a segment",
Message: fmt.Sprintf("invalid information in a segment: %v", err),
}
}

Expand Down
4 changes: 2 additions & 2 deletions grossAmountRemittanceDocument.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (gard *GrossAmountRemittanceDocument) Parse(record string) error {
gard.RemittanceAmount.Amount = value
length += read

if !gard.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := gard.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions institutionAccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (iAccount *InstitutionAccount) Parse(record string) error {
iAccount.CoverPayment.SwiftLineFive = value
length += read

if !iAccount.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := iAccount.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions instructedAmount.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func (ia *InstructedAmount) Parse(record string) error {
ia.Amount = value
length += read

if !ia.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := ia.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions instructingFI.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func (ifi *InstructingFI) Parse(record string) error {
ifi.FinancialInstitution.Address.AddressLineThree = value
length += read

if !ifi.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := ifi.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions intermediaryInstitution.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (ii *IntermediaryInstitution) Parse(record string) error {
ii.CoverPayment.SwiftLineFive = value
length += read

if !ii.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := ii.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions localInstrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (li *LocalInstrument) Parse(record string) error {
li.ProprietaryCode = value
length += read

if !li.verifyDataWithReadLength(record, length) {
return NewTagMaxLengthErr()
if err := li.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}

return nil
Expand Down
Loading

0 comments on commit df51ffb

Please sign in to comment.