Skip to content

Commit

Permalink
fix: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Wompipomp committed Jan 3, 2025
1 parent df32535 commit 4f2b945
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
7 changes: 5 additions & 2 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,16 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
}

if len(conditions) > 0 {
pkgresource.SetConditions(rsp, conditions, log)
err := pkgresource.SetConditions(rsp, conditions, log)
if err != nil {
return rsp, nil
}
}

if len(events) > 0 {
err := pkgresource.SetEvents(rsp, events)
if err != nil {
return rsp, err
return rsp, nil
}
}

Expand Down
11 changes: 9 additions & 2 deletions pkg/resource/conditions.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package resource

import (
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/logging"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/response"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)
Expand Down Expand Up @@ -78,10 +81,14 @@ func transformTarget(t *BindingTarget) *fnv1.Target {
return fnv1.Target_TARGET_COMPOSITE.Enum()
}

func SetConditions(rsp *fnv1.RunFunctionResponse, cr ConditionResources, log logging.Logger) {
func SetConditions(rsp *fnv1.RunFunctionResponse, cr ConditionResources, log logging.Logger) error {
conditionsSet := map[string]bool{}
// All matchConditions matched, set the desired conditions.
for _, cs := range cr {
if xpv1.IsSystemConditionType(xpv1.ConditionType(cs.Condition.Type)) {
response.Fatal(rsp, errors.Errorf("cannot set ClaimCondition type: %s is a reserved Crossplane Condition", cs.Condition.Type))
return errors.New("error updating response")
}
if conditionsSet[cs.Condition.Type] && (cs.Force == nil || !*cs.Force) {
// The condition is already set and this setter is not forceful.
log.Debug("skipping because condition is already set and setCondition is not forceful")
Expand All @@ -90,8 +97,8 @@ func SetConditions(rsp *fnv1.RunFunctionResponse, cr ConditionResources, log log
log.Debug("setting condition")

c := transformCondition(cs)

rsp.Conditions = append(rsp.Conditions, c)
conditionsSet[cs.Condition.Type] = true
}
return nil
}
4 changes: 3 additions & 1 deletion pkg/resource/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resource
import (
"github.com/crossplane/crossplane-runtime/pkg/errors"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/response"
"k8s.io/utils/ptr"
)

Expand Down Expand Up @@ -45,7 +46,8 @@ func SetEvents(rsp *fnv1.RunFunctionResponse, ers EventResources) error {
for _, er := range ers {
r, err := transformEvent(er)
if err != nil {
return err
response.Fatal(rsp, err)
return errors.New("error updating response")
}
rsp.Results = append(rsp.Results, r)
}
Expand Down

0 comments on commit 4f2b945

Please sign in to comment.