Repository: datreeio/datree
Pull request #477 (2022-03-13 11:42:07)

feat: report unexpected errors to server


Patch:
diff --git a/bl/errorReporter/errorReporter.go b/bl/errorReporter/errorReporter.go
index 30058c0..0ec7918 100644
--- a/bl/errorReporter/errorReporter.go
+++ b/bl/errorReporter/errorReporter.go
@@ -12,9 +12,14 @@ import (
 	"github.com/datreeio/datree/pkg/localConfig"
 )
 
-func ReportCliError(panicErr interface{}) {
+func ReportCliPanicError(panicErr interface{}) {
 	reporter := NewErrorReporter(cliClient.NewCliClient(deploymentConfig.URL), localConfig.NewLocalConfig(), printer.CreateNewPrinter())
-	reporter.ReportCliError(panicErr)
+	reporter.ReportCliError(panicErr, "/report-cli-panic-error")
+}
+
+func ReportCliUnexpectedError(unexpectedError error) {
+	reporter := NewErrorReporter(cliClient.NewCliClient(deploymentConfig.URL), localConfig.NewLocalConfig(), printer.CreateNewPrinter())
+	reporter.ReportCliError(unexpectedError, "/report-cli-unexpected-error")
 }
 
 type LocalConfig interface {
@@ -22,7 +27,7 @@ type LocalConfig interface {
 }
 
 type CliClient interface {
-	ReportCliError(reportCliErrorRequest cliClient.ReportCliErrorRequest) (StatusCode int, Error error)
+	ReportCliError(reportCliErrorRequest cliClient.ReportCliErrorRequest, uri string) (StatusCode int, Error error)
 }
 
 type Printer interface {
@@ -43,7 +48,7 @@ func NewErrorReporter(client CliClient, localConfig LocalConfig, printer Printer
 	}
 }
 
-func (reporter *ErrorReporter) ReportCliError(panicErr interface{}) {
+func (reporter *ErrorReporter) ReportCliError(panicErr interface{}, uri string) {
 	errorMessage := parsePanicError(panicErr)
 	cliId := reporter.getCliId()
 	_, err := reporter.client.ReportCliError(cliClient.ReportCliErrorRequest{
@@ -52,7 +57,7 @@ func (reporter *ErrorReporter) ReportCliError(panicErr interface{}) {
 		CliVersion:   cmd.CliVersion,
 		ErrorMessage: errorMessage,
 		StackTrace:   string(debug.Stack()),
-	})
+	}, uri)
 	if err != nil {
 		// do nothing
 	}
diff --git a/main.go b/main.go
index e949f99..b15b678 100644
--- a/main.go
+++ b/main.go
@@ -16,7 +16,7 @@ func main() {
 	// global error handling
 	defer func() {
 		if panicErr := recover(); panicErr != nil {
-			errorReporter.ReportCliError(panicErr)
+			errorReporter.ReportCliPanicError(panicErr)
 			os.Exit(DEFAULT_ERR_EXIT_CODE)
 		}
 	}()
@@ -25,6 +25,7 @@ func main() {
 		if errors.Is(err, test.ViolationsFoundError) {
 			os.Exit(VIOLATIONS_FOUND_EXIT_CODE)
 		}
+		errorReporter.ReportCliUnexpectedError(err)
 		os.Exit(DEFAULT_ERR_EXIT_CODE)
 	}
 }
diff --git a/pkg/cliClient/reportCliError.go b/pkg/cliClient/reportCliError.go
index 5c591e9..2747f06 100644
--- a/pkg/cliClient/reportCliError.go
+++ b/pkg/cliClient/reportCliError.go
@@ -12,11 +12,11 @@ type ReportCliErrorRequest struct {
 	StackTrace   string `json:"stackTrace"`
 }
 
-func (c *CliClient) ReportCliError(reportCliErrorRequest ReportCliErrorRequest) (StatusCode int, Error error) {
+func (c *CliClient) ReportCliError(reportCliErrorRequest ReportCliErrorRequest, uri string) (StatusCode int, Error error) {
 	headers := map[string]string{}
 	res, err := c.httpClient.Request(
 		http.MethodPost,
-		"/cli/public/report-cli-error",
+		"/cli/public/"+uri,
 		reportCliErrorRequest,
 		headers,
 	)


Test patch:
diff --git a/bl/errorReporter/errorReporter_test.go b/bl/errorReporter/errorReporter_test.go
index 6dab9ca..d825eda 100644
--- a/bl/errorReporter/errorReporter_test.go
+++ b/bl/errorReporter/errorReporter_test.go
@@ -17,7 +17,7 @@ type mockCliClient struct {
 	mock.Mock
 }
 
-func (m *mockCliClient) ReportCliError(reportCliErrorRequest cliClient.ReportCliErrorRequest) (StatusCode int, Error error) {
+func (m *mockCliClient) ReportCliError(reportCliErrorRequest cliClient.ReportCliErrorRequest, uri string) (StatusCode int, Error error) {
 	m.Called(reportCliErrorRequest)
 	return 201, nil
 }
@@ -73,7 +73,7 @@ func TestErrorReporter(t *testing.T) {
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			errorReporter.ReportCliError(tt.args.panicErr)
+			errorReporter.ReportCliError(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)