Repository: datreeio/datree
Pull request #482 (2022-03-13 16:12:52)

refactor: error reporter


Patch:
diff --git a/bl/errorReporter/errorReporter.go b/bl/errorReporter/errorReporter.go
index c0b7131..6e88ea2 100644
--- a/bl/errorReporter/errorReporter.go
+++ b/bl/errorReporter/errorReporter.go
@@ -1,27 +1,15 @@
 package errorReporter
 
 import (
-	"fmt"
 	"runtime/debug"
 
-	"github.com/datreeio/datree/pkg/printer"
+	"github.com/datreeio/datree/pkg/utils"
 
 	"github.com/datreeio/datree/cmd"
 	"github.com/datreeio/datree/pkg/cliClient"
-	"github.com/datreeio/datree/pkg/deploymentConfig"
 	"github.com/datreeio/datree/pkg/localConfig"
 )
 
-func ReportCliPanicError(panicErr interface{}) {
-	reporter := NewErrorReporter(cliClient.NewCliClient(deploymentConfig.URL), localConfig.NewLocalConfigClient(&cliClient.CliClient{}), printer.CreateNewPrinter())
-	reporter.ReportCliError(panicErr, "/report-cli-panic-error")
-}
-
-func ReportCliUnexpectedError(unexpectedError error) {
-	reporter := NewErrorReporter(cliClient.NewCliClient(deploymentConfig.URL), localConfig.NewLocalConfigClient(&cliClient.CliClient{}), printer.CreateNewPrinter())
-	reporter.ReportCliError(unexpectedError, "/report-cli-unexpected-error")
-}
-
 type LocalConfig interface {
 	GetLocalConfiguration() (*localConfig.LocalConfig, error)
 }
@@ -30,26 +18,28 @@ type CliClient interface {
 	ReportCliError(reportCliErrorRequest cliClient.ReportCliErrorRequest, uri string) (StatusCode int, Error error)
 }
 
-type Printer interface {
-	PrintMessage(messageText string, messageColor string)
-}
-
 type ErrorReporter struct {
-	config  LocalConfig
-	client  CliClient
-	printer Printer
+	config LocalConfig
+	client CliClient
 }
 
-func NewErrorReporter(client CliClient, localConfig LocalConfig, printer Printer) *ErrorReporter {
+func NewErrorReporter(client CliClient, localConfig LocalConfig) *ErrorReporter {
 	return &ErrorReporter{
-		client:  client,
-		config:  localConfig,
-		printer: printer,
+		client: client,
+		config: localConfig,
 	}
 }
 
-func (reporter *ErrorReporter) ReportCliError(panicErr interface{}, uri string) {
-	errorMessage := parsePanicError(panicErr)
+func (reporter *ErrorReporter) ReportPanicError(panicErr interface{}) {
+	reporter.ReportError(panicErr, "/report-cli-panic-error")
+}
+
+func (reporter *ErrorReporter) ReportUnexpectedError(unexpectedError error) {
+	reporter.ReportError(unexpectedError, "/report-cli-unexpected-error")
+}
+
+func (reporter *ErrorReporter) ReportError(error interface{}, uri string) {
+	errorMessage := utils.ParseErrorToString(error)
 	cliId := reporter.getCliId()
 	_, err := reporter.client.ReportCliError(cliClient.ReportCliErrorRequest{
 		ClientId:     cliId,
@@ -61,7 +51,6 @@ func (reporter *ErrorReporter) ReportCliError(panicErr interface{}, uri string)
 	if err != nil {
 		// do nothing
 	}
-	reporter.printer.PrintMessage(fmt.Sprintf("Unexpected error: %s\n", errorMessage), "error")
 }
 
 func (reporter *ErrorReporter) getCliId() (cliId string) {
@@ -78,14 +67,3 @@ func (reporter *ErrorReporter) getCliId() (cliId string) {
 		return config.Token
 	}
 }
-
-func parsePanicError(panicErr interface{}) string {
-	switch panicErr := panicErr.(type) {
-	case string:
-		return panicErr
-	case error:
-		return panicErr.Error()
-	default:
-		return fmt.Sprintf("%v", panicErr)
-	}
-}
diff --git a/main.go b/main.go
index b15b678..803369a 100644
--- a/main.go
+++ b/main.go
@@ -2,8 +2,15 @@ package main
 
 import (
 	"errors"
+	"fmt"
 	"os"
 
+	"github.com/datreeio/datree/pkg/cliClient"
+	"github.com/datreeio/datree/pkg/deploymentConfig"
+	"github.com/datreeio/datree/pkg/localConfig"
+	"github.com/datreeio/datree/pkg/printer"
+	"github.com/datreeio/datree/pkg/utils"
+
 	"github.com/datreeio/datree/bl/errorReporter"
 	"github.com/datreeio/datree/cmd"
 	"github.com/datreeio/datree/cmd/test"
@@ -13,10 +20,17 @@ const DEFAULT_ERR_EXIT_CODE = 1
 const VIOLATIONS_FOUND_EXIT_CODE = 2
 
 func main() {
-	// global error handling
+	cliClient := cliClient.NewCliClient(deploymentConfig.URL)
+	localConfig := localConfig.NewLocalConfigClient(cliClient)
+
+	reporter := errorReporter.NewErrorReporter(cliClient, localConfig)
+	globalPrinter := printer.CreateNewPrinter()
+
 	defer func() {
 		if panicErr := recover(); panicErr != nil {
-			errorReporter.ReportCliPanicError(panicErr)
+			reporter.ReportPanicError(panicErr)
+
+			globalPrinter.PrintMessage(fmt.Sprintf("Unexpected error: %s\n", utils.ParseErrorToString(panicErr)), "error")
 			os.Exit(DEFAULT_ERR_EXIT_CODE)
 		}
 	}()
@@ -25,7 +39,7 @@ func main() {
 		if errors.Is(err, test.ViolationsFoundError) {
 			os.Exit(VIOLATIONS_FOUND_EXIT_CODE)
 		}
-		errorReporter.ReportCliUnexpectedError(err)
+		reporter.ReportUnexpectedError(err)
 		os.Exit(DEFAULT_ERR_EXIT_CODE)
 	}
 }
diff --git a/pkg/utils/errors.go b/pkg/utils/errors.go
new file mode 100644
index 0000000..06d325d
--- /dev/null
+++ b/pkg/utils/errors.go
@@ -0,0 +1,14 @@
+package utils
+
+import "fmt"
+
+func ParseErrorToString(err interface{}) string {
+	switch panicErr := err.(type) {
+	case string:
+		return panicErr
+	case error:
+		return panicErr.Error()
+	default:
+		return fmt.Sprintf("%v", panicErr)
+	}
+}


Test patch:
diff --git a/bl/errorReporter/errorReporter_test.go b/bl/errorReporter/errorReporter_test.go
index 6543f0e..a0404fd 100644
--- a/bl/errorReporter/errorReporter_test.go
+++ b/bl/errorReporter/errorReporter_test.go
@@ -61,19 +61,16 @@ 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,
-		printer: mockedPrinter,
+		client: mockedCliClient,
+		config: mockedConfig,
 	}
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			errorReporter.ReportCliError(tt.args.panicErr, "/report-cli-panic-error")
+			errorReporter.ReportError(tt.args.panicErr, "/report-cli-panic-error")
 			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)