Improve record value error handling & cleanup errors in rest
This commit is contained in:
parent
288e516986
commit
4d2b0fc7d3
@ -9,27 +9,22 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/compose/service/values"
|
||||
"github.com/cortezaproject/corteza-server/pkg/payload"
|
||||
|
||||
"github.com/titpetric/factory/resputil"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/compose/decoder"
|
||||
"github.com/cortezaproject/corteza-server/compose/encoder"
|
||||
"github.com/cortezaproject/corteza-server/compose/repository"
|
||||
"github.com/cortezaproject/corteza-server/compose/rest/request"
|
||||
"github.com/cortezaproject/corteza-server/compose/service"
|
||||
"github.com/cortezaproject/corteza-server/compose/service/event"
|
||||
"github.com/cortezaproject/corteza-server/compose/service/values"
|
||||
"github.com/cortezaproject/corteza-server/compose/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/corredor"
|
||||
"github.com/cortezaproject/corteza-server/pkg/mime"
|
||||
"github.com/cortezaproject/corteza-server/pkg/payload"
|
||||
"github.com/cortezaproject/corteza-server/pkg/rh"
|
||||
)
|
||||
|
||||
var _ = errors.Wrap
|
||||
|
||||
type (
|
||||
recordPayload struct {
|
||||
*types.Record
|
||||
@ -163,14 +158,13 @@ func (ctrl *Record) Create(ctx context.Context, r *request.RecordCreate) (interf
|
||||
// Validate returned bulk operations
|
||||
for _, o := range oob {
|
||||
if o.LinkBy != "" && len(oo) == 0 {
|
||||
return nil, errors.New("missing parent record definition")
|
||||
return nil, fmt.Errorf("missing parent record definition")
|
||||
}
|
||||
}
|
||||
oo = append(oo, oob...)
|
||||
|
||||
rr, err := ctrl.record.With(ctx).Bulk(oo...)
|
||||
|
||||
if rve, is := err.(*types.RecordValueErrorSet); is && !rve.IsValid() {
|
||||
if rve := types.IsRecordValueErrorSet(err); rve != nil {
|
||||
return ctrl.handleValidationError(rve), nil
|
||||
}
|
||||
|
||||
@ -212,14 +206,14 @@ func (ctrl *Record) Update(ctx context.Context, r *request.RecordUpdate) (interf
|
||||
// Validate returned bulk operations
|
||||
for _, o := range oob {
|
||||
if o.LinkBy != "" && len(oo) == 0 {
|
||||
return nil, errors.New("missing parent record definition")
|
||||
return nil, fmt.Errorf("missing parent record definition")
|
||||
}
|
||||
}
|
||||
oo = append(oo, oob...)
|
||||
|
||||
rr, err := ctrl.record.With(ctx).Bulk(oo...)
|
||||
|
||||
if rve, is := err.(*types.RecordValueErrorSet); is && !rve.IsValid() {
|
||||
if rve := types.IsRecordValueErrorSet(err); rve != nil {
|
||||
return ctrl.handleValidationError(rve), nil
|
||||
}
|
||||
|
||||
@ -232,7 +226,7 @@ func (ctrl *Record) Delete(ctx context.Context, r *request.RecordDelete) (interf
|
||||
|
||||
func (ctrl *Record) BulkDelete(ctx context.Context, r *request.RecordBulkDelete) (interface{}, error) {
|
||||
if r.Truncate {
|
||||
return nil, errors.New("pending implementation")
|
||||
return nil, fmt.Errorf("pending implementation")
|
||||
}
|
||||
|
||||
return resputil.OK(), ctrl.record.With(ctx).DeleteByID(
|
||||
@ -459,7 +453,7 @@ func (ctrl Record) Exec(ctx context.Context, r *request.RecordExec) (interface{}
|
||||
aa.Get("group"),
|
||||
)
|
||||
default:
|
||||
return nil, errors.New("unknown procedure")
|
||||
return nil, fmt.Errorf("unknown procedure")
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
|
||||
@ -2,7 +2,6 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@ -503,25 +502,24 @@ func (svc record) Bulk(oo ...*types.BulkRecordOperation) (rr types.RecordSet, er
|
||||
r, err = svc.delete(r.NamespaceID, r.ModuleID, r.ID)
|
||||
}
|
||||
|
||||
if errors.Is(err, RecordErrValueInput()) {
|
||||
wrappedRve := errors.Unwrap(err)
|
||||
if rve, ok := wrappedRve.(*types.RecordValueErrorSet); ok {
|
||||
// Attach additional meta to each value error for FE identification
|
||||
for _, re := range rve.Set {
|
||||
re.Meta["resource"] = res
|
||||
re.Meta["item"] = ctr[res]
|
||||
if rve := types.IsRecordValueErrorSet(err); rve != nil {
|
||||
// Attach additional meta to each value error for FE identification
|
||||
for _, re := range rve.Set {
|
||||
re.Meta["resource"] = res
|
||||
re.Meta["item"] = ctr[res]
|
||||
|
||||
rves.Push(re)
|
||||
}
|
||||
|
||||
// log record value error for this record
|
||||
_ = svc.recordAction(svc.ctx, aProp, action, err)
|
||||
|
||||
// do not return errors just yet, values on other records from the payload (if any)
|
||||
// might have errors too
|
||||
continue
|
||||
rves.Push(re)
|
||||
}
|
||||
} else if err != nil {
|
||||
|
||||
// log record value error for this record
|
||||
_ = svc.recordAction(svc.ctx, aProp, action, err)
|
||||
|
||||
// do not return errors just yet, values on other records from the payload (if any)
|
||||
// might have errors too
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return svc.recordAction(svc.ctx, aProp, action, err)
|
||||
}
|
||||
|
||||
@ -531,7 +529,7 @@ func (svc record) Bulk(oo ...*types.BulkRecordOperation) (rr types.RecordSet, er
|
||||
|
||||
if !rves.IsValid() {
|
||||
// Any errors gathered?
|
||||
return rves
|
||||
return RecordErrValueInput().Wrap(rves)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package types
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
RecordValueError struct {
|
||||
@ -25,3 +28,23 @@ func (v *RecordValueErrorSet) IsValid() bool {
|
||||
func (v *RecordValueErrorSet) Error() string {
|
||||
return fmt.Sprintf("%d issue(s) found", len(v.Set))
|
||||
}
|
||||
|
||||
// IsRecordValueErrorSet tests if given error is RecordValueErrorSet (or it wraps it) and it has errors
|
||||
// If not is not (or !IsValid), it return nil!
|
||||
func IsRecordValueErrorSet(err error) *RecordValueErrorSet {
|
||||
for {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if rve, ok := err.(*RecordValueErrorSet); ok {
|
||||
if rve.IsValid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return rve
|
||||
}
|
||||
|
||||
err = errors.Unwrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user