Skip to content

Commit

Permalink
Added error wrapping for compose record wf functions
Browse files Browse the repository at this point in the history
  • Loading branch information
petergrlica authored and Fajfa committed Nov 30, 2023
1 parent db9d459 commit 47beebb
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions server/compose/automation/records_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package automation

import (
"context"
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"

"github.com/cortezaproject/corteza/server/compose/types"
. "github.com/cortezaproject/corteza/server/pkg/expr"
Expand Down Expand Up @@ -272,13 +276,14 @@ func (h recordsHandler) new(ctx context.Context, args *recordsNewArgs) (*records

func (h recordsHandler) create(ctx context.Context, args *recordsCreateArgs) (results *recordsCreateResults, err error) {
results = &recordsCreateResults{}
results.Record, _, err = h.rec.Create(ctx, args.Record)
results.Record, err = wrapRecordValueErrorSet(h.rec.Create(ctx, args.Record))

return
}

func (h recordsHandler) update(ctx context.Context, args *recordsUpdateArgs) (results *recordsUpdateResults, err error) {
results = &recordsUpdateResults{}
results.Record, _, err = h.rec.Update(ctx, args.Record)
results.Record, err = wrapRecordValueErrorSet(h.rec.Update(ctx, args.Record))
return
}

Expand Down Expand Up @@ -398,3 +403,36 @@ func (i *recordSetIterator) Next(context.Context, *Vars) (out *Vars, err error)
i.ptr++
return out, nil
}

func wrapRecordValueErrorSet(rr *types.Record, res *types.RecordValueErrorSet, ee error) (r *types.Record, err error) {
var (
ss string
j []byte
e error
)

r = rr
err = ee

if res = types.IsRecordValueErrorSet(err); res == nil {
return
}

if !res.Safe() {
return rr, err
}

for _, vv := range res.Set {
if j, e = json.Marshal(vv.Meta); e != nil {
continue
}

ss = fmt.Sprintf("%s, %s (%s)", ss, strings.ToLower(vv.Message), j)

}

re := regexp.MustCompile("^,\\s")
ss = re.ReplaceAllString(ss, "")

return rr, errors.New(ss)
}

0 comments on commit 47beebb

Please sign in to comment.