Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error wrapping for compose record wf functions #1571

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}