Commit d7c82208 authored by Ben Zaad's avatar Ben Zaad
Browse files

feat: report unexpected errors to server

parent 08069ea5
Showing with 16 additions and 10 deletions
+16 -10
......@@ -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
}
......
......@@ -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)
......
......@@ -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)
}
}
......@@ -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,
)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment