This is an automated email from the ASF dual-hosted git repository.

pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new cc8d518c2 fix: won't panic on error handler validation failure
cc8d518c2 is described below

commit cc8d518c206babe13aec9af75bd641869343d362
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Wed Apr 12 15:17:25 2023 +0200

    fix: won't panic on error handler validation failure
    
    Introduced a validation for each kind of error handler.
    
    Closes #3586
---
 pkg/apis/camel/v1alpha1/error_handler_types_support.go | 15 +++++++++++++++
 pkg/controller/kameletbinding/error_handler.go         |  6 ++++--
 pkg/controller/kameletbinding/error_handler_test.go    | 10 ++++++++--
 3 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/pkg/apis/camel/v1alpha1/error_handler_types_support.go 
b/pkg/apis/camel/v1alpha1/error_handler_types_support.go
index dfda4c713..79f473a70 100644
--- a/pkg/apis/camel/v1alpha1/error_handler_types_support.go
+++ b/pkg/apis/camel/v1alpha1/error_handler_types_support.go
@@ -19,6 +19,7 @@ package v1alpha1
 
 import (
        "encoding/json"
+       "fmt"
 )
 
 // +kubebuilder:object:generate=false
@@ -28,6 +29,7 @@ type ErrorHandler interface {
        Type() ErrorHandlerType
        Endpoint() *Endpoint
        Configuration() (map[string]interface{}, error)
+       Validate() error
 }
 
 // baseErrorHandler is the base used for the Error Handler hierarchy
@@ -49,6 +51,11 @@ func (e baseErrorHandler) Configuration() 
(map[string]interface{}, error) {
        return nil, nil
 }
 
+// Validate --
+func (e baseErrorHandler) Validate() error {
+       return nil
+}
+
 // ErrorHandlerNone --
 type ErrorHandlerNone struct {
        baseErrorHandler
@@ -126,3 +133,11 @@ func (e ErrorHandlerSink) Configuration() 
(map[string]interface{}, error) {
 
        return properties, err
 }
+
+// Validate --
+func (e ErrorHandlerSink) Validate() error {
+       if e.DLCEndpoint == nil {
+               return fmt.Errorf("Missing endpoint in Error Handler Sink")
+       }
+       return nil
+}
diff --git a/pkg/controller/kameletbinding/error_handler.go 
b/pkg/controller/kameletbinding/error_handler.go
index c85829a52..231c5b49a 100644
--- a/pkg/controller/kameletbinding/error_handler.go
+++ b/pkg/controller/kameletbinding/error_handler.go
@@ -79,8 +79,10 @@ func parseErrorHandler(rawMessage v1alpha1.RawMessage) 
(v1alpha1.ErrorHandler, e
                        return nil, errors.Errorf("Unknown error handler type 
%s", errHandlType)
                }
 
-               err := json.Unmarshal(errHandlValue, dst)
-               if err != nil {
+               if err = json.Unmarshal(errHandlValue, dst); err != nil {
+                       return nil, err
+               }
+               if err = dst.Validate(); err != nil {
                        return nil, err
                }
 
diff --git a/pkg/controller/kameletbinding/error_handler_test.go 
b/pkg/controller/kameletbinding/error_handler_test.go
index 6abbf2187..02607dda4 100644
--- a/pkg/controller/kameletbinding/error_handler_test.go
+++ b/pkg/controller/kameletbinding/error_handler_test.go
@@ -18,7 +18,6 @@ limitations under the License.
 package kameletbinding
 
 import (
-       "fmt"
        "testing"
 
        "github.com/apache/camel-k/v2/pkg/apis/camel/v1alpha1"
@@ -64,7 +63,6 @@ func TestParseErrorHandlerLogWithParametersDoesSucceed(t 
*testing.T) {
 }
 
 func TestParseErrorHandlerSinkDoesSucceed(t *testing.T) {
-       fmt.Println("Test")
        sinkErrorHandler, err := parseErrorHandler(
                []byte(`{"sink": {"endpoint": {"uri": "someUri"}}}`),
        )
@@ -101,3 +99,11 @@ func TestParseErrorHandlerSinkWithParametersDoesSucceed(t 
*testing.T) {
        assert.Equal(t, "value1", 
parameters["camel.beans.defaultErrorHandler.param1"])
        assert.Equal(t, "value2", 
parameters["camel.beans.defaultErrorHandler.param2"])
 }
+
+func TestParseErrorHandlerSinkFail(t *testing.T) {
+       _, err := parseErrorHandler(
+               []byte(`{"sink": {"ref": {"uri": "someUri"}}}`),
+       )
+       assert.NotNil(t, err)
+       assert.Equal(t, "Missing endpoint in Error Handler Sink", err.Error())
+}

Reply via email to