Skip to content

Commit

Permalink
fix up error logic on file level
Browse files Browse the repository at this point in the history
  • Loading branch information
mfdeveloper508 committed Aug 3, 2023
1 parent 2b190ce commit 3f7c5e7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
17 changes: 8 additions & 9 deletions pkg/file/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,21 @@ func (r *FunctionalGroup) Validate(groupRule *rules.GroupRule) error {
{
err = r.GS.Validate(&gsRule.Elements)
if err != nil {
util.AppendErrorStack(err, util.GetStructName(r))
return err
}
}

// Validating transaction sets
{
if len(r.TransactionSets) == 0 {
return util.NewFindElementError("transaction set")
return util.NewFindRuleError(util.GetStructName(r))
}

for index := 0; index < len(r.TransactionSets); index++ {
set := r.TransactionSets[index]
if err = set.Validate(&groupRule.Trans); err != nil {
util.AppendErrorStack(err, util.GetStructName(r))
return err
}
}
Expand All @@ -73,14 +75,10 @@ func (r *FunctionalGroup) Validate(groupRule *rules.GroupRule) error {

// Validating GE Segment
geRule := groupRule.GE
if rules.IsMaskRequired(geRule.Mask) && r.GE == nil {
return errors.New("ge segment is required segment")
}

if r.GE != nil {
err = r.GE.Validate(&geRule.Elements)
if err != nil && rules.IsMaskRequired(geRule.Mask) {
return errors.New("unable to validate ge segment")
return err
}
}

Expand All @@ -103,7 +101,7 @@ func (r *FunctionalGroup) Validate(groupRule *rules.GroupRule) error {

func (r *FunctionalGroup) Parse(data string, args ...string) (int, error) {
if r.rule == nil {
return 0, errors.New("please specify rules for this group")
return 0, util.NewFindRuleError(util.GetStructName(r))
}

var size, read int
Expand All @@ -116,7 +114,7 @@ func (r *FunctionalGroup) Parse(data string, args ...string) (int, error) {
size, err = r.GS.Parse(data[read:], args...)
if err != nil {
util.AppendErrorSegmentLine(err, data[read:], args...)
return 0, util.UpdateErrorReason(err)
return 0, err
} else {
read += size
}
Expand All @@ -133,6 +131,7 @@ func (r *FunctionalGroup) Parse(data string, args ...string) (int, error) {
} else {
line := data[read:]
if len(r.TransactionSets) == 0 && (len(line) > 2 && line[0:2] == "ST") {
util.AppendErrorStack(err, util.GetStructName(r))
return 0, err
}
}
Expand All @@ -145,7 +144,7 @@ func (r *FunctionalGroup) Parse(data string, args ...string) (int, error) {
size, err = newGE.Parse(data[read:], args...)
if err != nil && rules.IsMaskRequired(geRule.Mask) {
util.AppendErrorSegmentLine(err, data[read:], args...)
return 0, util.UpdateErrorReason(err)
return 0, err
} else if err == nil {
read += size
if s, ok := newGE.(*segments.GE); ok {
Expand Down
17 changes: 6 additions & 11 deletions pkg/file/interchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (r *Interchange) Validate(validateRule *rules.InterchangeRule) error {
}

if changeRule == nil {
return util.NewFindRuleError("interchange")
return util.NewFindRuleError(util.GetStructName(r))
}

var err error
Expand All @@ -79,7 +79,7 @@ func (r *Interchange) Validate(validateRule *rules.InterchangeRule) error {
{
err = r.ISA.Validate(&isaRule.Elements)
if err != nil {
return err
return util.UpdateErrorReason(err)
}
}

Expand All @@ -92,22 +92,17 @@ func (r *Interchange) Validate(validateRule *rules.InterchangeRule) error {
for index := 0; index < len(r.FunctionalGroups); index++ {
group := r.FunctionalGroups[index]
if err = group.Validate(&changeRule.Group); err != nil {
return err
return util.UpdateErrorReason(err)
}
}

}

// Validating IEA Segment
ieaRule := changeRule.IEA
if rules.IsMaskRequired(ieaRule.Mask) && r.IEA == nil {
return errors.New("iea segment is required segment")
}

if r.IEA != nil {
err = r.IEA.Validate(&ieaRule.Elements)
if err != nil && rules.IsMaskRequired(ieaRule.Mask) {
return err
return util.UpdateErrorReason(err)
}
}

Expand Down Expand Up @@ -140,7 +135,7 @@ func (r *Interchange) Validate(validateRule *rules.InterchangeRule) error {

func (r *Interchange) Parse(data string) (int, error) {
if r.rule == nil {
return 0, errors.New("please specify rules for this group")
return 0, util.NewFindRuleError(util.GetStructName(r))
}

var size, read int
Expand Down Expand Up @@ -170,7 +165,7 @@ func (r *Interchange) Parse(data string) (int, error) {
} else {
line := data[read:]
if len(r.FunctionalGroups) == 0 && (len(line) > 2 && line[0:2] == "GS") {
return 0, err
return 0, util.UpdateErrorReason(err)
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/file/transactionset.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,21 @@ func (r *TransactionSet) Parse(data string, args ...string) (int, error) {
{
r.ST.SetRule(&stRule.Elements)
size, err = r.ST.Parse(data[read:], args...)
read += size
if err != nil {
util.AppendErrorStack(err, util.GetStructName(r))
return 0, err
} else {
read += size
return read, err
}
}

// Parsing Composite
{
r.Composite.SetRule(&r.rule.Composite)
size, err = r.Composite.Parse(data[read:], args...)
read += size
if err != nil {
util.AppendErrorStack(err, util.GetStructName(r))
return 0, err
} else {
read += size
return read, err
}
}

Expand All @@ -129,7 +127,7 @@ func (r *TransactionSet) Parse(data string, args ...string) (int, error) {
size, err = newSE.Parse(data[read:], args...)
if err != nil && rules.IsMaskRequired(seRule.Mask) {
util.AppendErrorStack(err, util.GetStructName(r))
return 0, err
return read, err
} else if err == nil {
read += size
if s, ok := newSE.(*segments.SE); ok {
Expand Down
2 changes: 1 addition & 1 deletion pkg/segments/fa1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestForFA1(t *testing.T) {

require.Equal(t, "FA1*~", seg.String())

in := "FA1*0019*~"
in := "FA1*0019~"
read, err := seg.Parse(in)
require.NoError(t, err)
require.Equal(t, len(in), read)
Expand Down

0 comments on commit 3f7c5e7

Please sign in to comment.