Repository: datreeio/datree
Pull request #465 (2022-03-06 15:56:09)

feat: change cli error reporting to token + clientId


Patch:
diff --git a/bl/errorReporter/errorReporter.go b/bl/errorReporter/errorReporter.go
index 60b28f4..30058c0 100644
--- a/bl/errorReporter/errorReporter.go
+++ b/bl/errorReporter/errorReporter.go
@@ -4,6 +4,8 @@ import (
 	"fmt"
 	"runtime/debug"
 
+	"github.com/datreeio/datree/pkg/printer"
+
 	"github.com/datreeio/datree/cmd"
 	"github.com/datreeio/datree/pkg/cliClient"
 	"github.com/datreeio/datree/pkg/deploymentConfig"
@@ -11,7 +13,7 @@ import (
 )
 
 func ReportCliError(panicErr interface{}) {
-	reporter := NewErrorReporter(cliClient.NewCliClient(deploymentConfig.URL), localConfig.NewLocalConfig())
+	reporter := NewErrorReporter(cliClient.NewCliClient(deploymentConfig.URL), localConfig.NewLocalConfig(), printer.CreateNewPrinter())
 	reporter.ReportCliError(panicErr)
 }
 
@@ -23,30 +25,38 @@ type CliClient interface {
 	ReportCliError(reportCliErrorRequest cliClient.ReportCliErrorRequest) (StatusCode int, Error error)
 }
 
+type Printer interface {
+	PrintMessage(messageText string, messageColor string)
+}
+
 type ErrorReporter struct {
-	config LocalConfig
-	client CliClient
+	config  LocalConfig
+	client  CliClient
+	printer Printer
 }
 
-func NewErrorReporter(client CliClient, localConfig LocalConfig) *ErrorReporter {
+func NewErrorReporter(client CliClient, localConfig LocalConfig, printer Printer) *ErrorReporter {
 	return &ErrorReporter{
-		client: client,
-		config: localConfig,
+		client:  client,
+		config:  localConfig,
+		printer: printer,
 	}
 }
 
 func (reporter *ErrorReporter) ReportCliError(panicErr interface{}) {
-	status, err := reporter.client.ReportCliError(cliClient.ReportCliErrorRequest{
-		Token:        reporter.getCliId(),
+	errorMessage := parsePanicError(panicErr)
+	cliId := reporter.getCliId()
+	_, err := reporter.client.ReportCliError(cliClient.ReportCliErrorRequest{
+		ClientId:     cliId,
+		Token:        cliId,
 		CliVersion:   cmd.CliVersion,
-		ErrorMessage: parsePanicError(panicErr),
+		ErrorMessage: errorMessage,
 		StackTrace:   string(debug.Stack()),
 	})
 	if err != nil {
-		fmt.Println("failed to report unexpected error:\nstatus code:", status, "\nerror:", err.Error())
-	} else {
-		fmt.Println("unexpected error reported")
+		// do nothing
 	}
+	reporter.printer.PrintMessage(fmt.Sprintf("Unexpected error: %s\n", errorMessage), "error")
 }
 
 func (reporter *ErrorReporter) getCliId() (cliId string) {
diff --git a/main.go b/main.go
index 1d6f0ba..fc1ae7a 100644
--- a/main.go
+++ b/main.go
@@ -17,7 +17,6 @@ func main() {
 	defer func() {
 		if panicErr := recover(); panicErr != nil {
 			errorReporter.ReportCliError(panicErr)
-			panic(panicErr)
 		}
 	}()
 
diff --git a/pkg/cliClient/reportCliError.go b/pkg/cliClient/reportCliError.go
index 1fcb0f3..5c591e9 100644
--- a/pkg/cliClient/reportCliError.go
+++ b/pkg/cliClient/reportCliError.go
@@ -5,6 +5,7 @@ import (
 )
 
 type ReportCliErrorRequest struct {
+	ClientId     string `json:"clientId"`
 	Token        string `json:"token"`
 	CliVersion   string `json:"cliVersion"`
 	ErrorMessage string `json:"errorMessage"`


Test patch:
diff --git a/bl/errorReporter/errorReporter_test.go b/bl/errorReporter/errorReporter_test.go
index add5411..6dab9ca 100644
--- a/bl/errorReporter/errorReporter_test.go
+++ b/bl/errorReporter/errorReporter_test.go
@@ -35,6 +35,15 @@ func (lc *mockConfig) GetLocalConfiguration() (*localConfig.ConfigContent, error
 	}, nil
 }
 
+// printer mock
+type mockPrinter struct {
+	mock.Mock
+}
+
+func (mp *mockPrinter) PrintMessage(messageText string, messageColor string) {
+	mp.Called()
+}
+
 // test type
 type ErrorReporterTestCaseArgs struct {
 	panicErr interface{}
@@ -52,11 +61,14 @@ func TestErrorReporter(t *testing.T) {
 	}
 	mockedCliClient := &mockCliClient{}
 	mockedConfig := &mockConfig{}
+	mockedPrinter := &mockPrinter{}
 	mockedCliClient.On("ReportCliError", mock.Anything).Return(nil)
 	mockedConfig.On("GetLocalConfiguration").Return(nil)
+	mockedPrinter.On("PrintMessage", mock.Anything, mock.Anything).Return()
 	errorReporter := &ErrorReporter{
-		client: mockedCliClient,
-		config: mockedConfig,
+		client:  mockedCliClient,
+		config:  mockedConfig,
+		printer: mockedPrinter,
 	}
 
 	for _, tt := range tests {
@@ -64,6 +76,7 @@ func TestErrorReporter(t *testing.T) {
 			errorReporter.ReportCliError(tt.args.panicErr)
 			reportCliErrorCalledArgs := (mockedCliClient.Calls[0].Arguments.Get(0)).(cliClient.ReportCliErrorRequest)
 			assert.Equal(t, tt.expected.ErrorMessage, reportCliErrorCalledArgs.ErrorMessage)
+			assert.Equal(t, tt.expected.ClientId, reportCliErrorCalledArgs.ClientId)
 			assert.Equal(t, tt.expected.Token, reportCliErrorCalledArgs.Token)
 			assert.Equal(t, tt.expected.CliVersion, reportCliErrorCalledArgs.CliVersion)
 		})
@@ -77,6 +90,7 @@ func reportErrorWithError() *ErrorReporterTestCase {
 			panicErr: errors.New("this is the error message"),
 		},
 		expected: cliClient.ReportCliErrorRequest{
+			ClientId:     "2qRg9jzJGcA73ftqEcXuBp",
 			Token:        "2qRg9jzJGcA73ftqEcXuBp",
 			CliVersion:   cmd.CliVersion,
 			ErrorMessage: "this is the error message",
@@ -92,6 +106,7 @@ func reportErrorWithStringError() *ErrorReporterTestCase {
 			panicErr: "this is the error message",
 		},
 		expected: cliClient.ReportCliErrorRequest{
+			ClientId:     "2qRg9jzJGcA73ftqEcXuBp",
 			Token:        "2qRg9jzJGcA73ftqEcXuBp",
 			CliVersion:   cmd.CliVersion,
 			ErrorMessage: "this is the error message",