Skip to content

Commit 4f2b945

Browse files
committed
fix: improve error handling
1 parent df32535 commit 4f2b945

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

fn.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,16 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
200200
}
201201

202202
if len(conditions) > 0 {
203-
pkgresource.SetConditions(rsp, conditions, log)
203+
err := pkgresource.SetConditions(rsp, conditions, log)
204+
if err != nil {
205+
return rsp, nil
206+
}
204207
}
205208

206209
if len(events) > 0 {
207210
err := pkgresource.SetEvents(rsp, events)
208211
if err != nil {
209-
return rsp, err
212+
return rsp, nil
210213
}
211214
}
212215

pkg/resource/conditions.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package resource
22

33
import (
4+
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
5+
"github.com/crossplane/crossplane-runtime/pkg/errors"
46
"github.com/crossplane/crossplane-runtime/pkg/logging"
57
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
8+
"github.com/crossplane/function-sdk-go/response"
69
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
710
"k8s.io/utils/ptr"
811
)
@@ -78,10 +81,14 @@ func transformTarget(t *BindingTarget) *fnv1.Target {
7881
return fnv1.Target_TARGET_COMPOSITE.Enum()
7982
}
8083

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

9299
c := transformCondition(cs)
93-
94100
rsp.Conditions = append(rsp.Conditions, c)
95101
conditionsSet[cs.Condition.Type] = true
96102
}
103+
return nil
97104
}

pkg/resource/events.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resource
33
import (
44
"github.com/crossplane/crossplane-runtime/pkg/errors"
55
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
6+
"github.com/crossplane/function-sdk-go/response"
67
"k8s.io/utils/ptr"
78
)
89

@@ -45,7 +46,8 @@ func SetEvents(rsp *fnv1.RunFunctionResponse, ers EventResources) error {
4546
for _, er := range ers {
4647
r, err := transformEvent(er)
4748
if err != nil {
48-
return err
49+
response.Fatal(rsp, err)
50+
return errors.New("error updating response")
4951
}
5052
rsp.Results = append(rsp.Results, r)
5153
}

0 commit comments

Comments
 (0)