-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
40 lines (35 loc) · 984 Bytes
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package utils
import (
"database/sql"
"errors"
"net/http"
"syscall"
"time"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
)
var sentryHandler = sentryhttp.New(sentryhttp.Options{})
// Verify if there is an error and, in this case, try to send it to Sentry
// and return it. If is a running test, the error isn't sent to Sentry.
func ManageError(err error) error {
if err == sql.ErrNoRows {
return err
}
if errors.Is(err, syscall.ECONNRESET) {
return err
}
if err != nil && sentry.CurrentHub().Client() != nil {
sentry.CaptureException(err)
sentry.Flush(2 * time.Second)
}
return err
}
// Encapsulate the handler with Sentry
func EnhanceSentryEvent(handler http.HandlerFunc) http.HandlerFunc {
return sentryHandler.HandleFunc(func(rw http.ResponseWriter, r *http.Request) {
if hub := sentry.GetHubFromContext(r.Context()); hub != nil {
hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
}
handler(rw, r)
})
}