Selaa lähdekoodia

Replace `*ierrors.Error` with actual data type in Sentry and Honeybadger integrations

DarthSim 2 kuukautta sitten
vanhempi
commit
6b7b3e9179
2 muutettua tiedostoa jossa 26 lisäystä ja 4 poistoa
  1. 9 1
      errorreport/honeybadger/honeybadger.go
  2. 17 3
      errorreport/sentry/sentry.go

+ 9 - 1
errorreport/honeybadger/honeybadger.go

@@ -2,11 +2,13 @@ package honeybadger
 
 import (
 	"net/http"
+	"reflect"
 	"strings"
 
 	"github.com/honeybadger-io/honeybadger-go"
 
 	"github.com/imgproxy/imgproxy/v3/config"
+	"github.com/imgproxy/imgproxy/v3/ierrors"
 )
 
 var (
@@ -42,5 +44,11 @@ func Report(err error, req *http.Request, meta map[string]any) {
 		extra[key] = v
 	}
 
-	honeybadger.Notify(err, req.URL, extra)
+	hbErr := honeybadger.NewError(err)
+
+	if e, ok := err.(*ierrors.Error); ok {
+		hbErr.Class = reflect.TypeOf(e.Unwrap()).String()
+	}
+
+	honeybadger.Notify(hbErr, req.URL, extra)
 }

+ 17 - 3
errorreport/sentry/sentry.go

@@ -40,8 +40,22 @@ func Report(err error, req *http.Request, meta map[string]any) {
 		hub.Scope().SetContext("Processing context", meta)
 	}
 
-	eventID := hub.CaptureException(err)
-	if eventID != nil {
-		hub.Flush(timeout)
+	// imgproxy wraps almost all errors into *ierrors.Error, so Sentry will show
+	// the same error type for all errors. We need to fix it.
+	//
+	// Instead of using hub.CaptureException(err), we need to create an event
+	// manually and replace `*ierrors.Error` with the wrapped error type
+	// (which is the previous exception type in the exception chain).
+	if event := hub.Client().EventFromException(err, sentry.LevelError); event != nil {
+		for i := 1; i < len(event.Exception); i++ {
+			if event.Exception[i].Type == "*ierrors.Error" {
+				event.Exception[i].Type = event.Exception[i-1].Type
+			}
+		}
+
+		eventID := hub.CaptureEvent(event)
+		if eventID != nil {
+			hub.Flush(timeout)
+		}
 	}
 }