diff --git a/routes/index.go b/routes/index.go index 92fd77f7f..e1a80aeef 100644 --- a/routes/index.go +++ b/routes/index.go @@ -1,6 +1,7 @@ package routes import ( + "bytes" "encoding/json" "fmt" "io" @@ -15,6 +16,7 @@ import ( "github.com/urfave/negroni" "github.com/stakwork/sphinx-tribes/auth" + "github.com/stakwork/sphinx-tribes/config" "github.com/stakwork/sphinx-tribes/db" "github.com/stakwork/sphinx-tribes/handlers" "github.com/stakwork/sphinx-tribes/utils" @@ -145,6 +147,49 @@ func getFromAuth(path string) (*extractResponse, error) { }, nil } +func sendEdgeListToJarvis(edgeList utils.EdgeList) error { + if config.JarvisUrl == "" || config.JarvisToken == "" { + fmt.Println("Jarvis configuration not found, skipping error reporting") + return nil + } + + jarvisURL := fmt.Sprintf("%s/node/edge/bulk", config.JarvisUrl) + + jsonData, err := json.Marshal(edgeList) + if err != nil { + fmt.Printf("Failed to marshal edge list: %v\n", err) + return nil + } + + req, err := http.NewRequest("POST", jarvisURL, bytes.NewBuffer(jsonData)) + if err != nil { + fmt.Printf("Failed to create Jarvis request: %v\n", err) + return nil + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("x-api-token", config.JarvisToken) + + client := &http.Client{ + Timeout: 10 * time.Second, + } + + resp, err := client.Do(req) + if err != nil { + fmt.Printf("Failed to send error to Jarvis: %v\n", err) + return err + } + defer resp.Body.Close() + + if resp.StatusCode >= 200 && resp.StatusCode < 300 { + fmt.Println("Successfully sent error to Jarvis") + return nil + } + + body, _ := io.ReadAll(resp.Body) + return fmt.Errorf("jarvis returned non-success status: %d, body: %s", resp.StatusCode, string(body)) +} + // Middleware to handle InternalServerError func internalServerErrorHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -168,7 +213,13 @@ func internalServerErrorHandler(next http.Handler) http.Handler { utils.PrettyPrintEdgeList(edgeList), ) - panic(http.StatusText(http.StatusInternalServerError)) + go func() { + if err := sendEdgeListToJarvis(edgeList); err != nil { + fmt.Printf("Error sending to Jarvis: %v\n", err) + } + }() + + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } }() diff --git a/utils/stacktrace.go b/utils/stacktrace.go index 9c141f4a6..38238b592 100644 --- a/utils/stacktrace.go +++ b/utils/stacktrace.go @@ -265,6 +265,9 @@ func parseStackTrace(stackTrace string) []string { frames = append(frames, line) } } + if len(frames) > 1 { + return frames[2:] + } return frames } diff --git a/utils/stacktrace_test.go b/utils/stacktrace_test.go index 99de1acf2..dee7b0c2b 100644 --- a/utils/stacktrace_test.go +++ b/utils/stacktrace_test.go @@ -95,7 +95,7 @@ func TestParseStackTrace(t *testing.T) { { name: "Normal Stack Trace", stackTrace: generateTestStackTrace(), - expectedFrames: 4, + expectedFrames: 2, }, { name: "Empty Stack Trace",