-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
294eaff
commit 3013272
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/getsentry/sentry-go" | ||
sentryiris "github.com/getsentry/sentry-go/iris" | ||
"github.com/kataras/iris" | ||
) | ||
|
||
func main() { | ||
_ = sentry.Init(sentry.ClientOptions{ | ||
Dsn: "https://363a337c11a64611be4845ad6e24f3ac@sentry.io/297378", | ||
Debug: true, | ||
AttachStacktrace: true, | ||
}) | ||
|
||
app := iris.Default() | ||
app.Use(sentryiris.New(sentryiris.Options{ | ||
Repanic: true, | ||
WaitForDelivery: true, | ||
}).Handle()) | ||
app.Get("/", func(ctx iris.Context) { | ||
panic("y tho") | ||
}) | ||
app.Run(iris.Addr(":3000")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package sentryiris | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/kataras/iris" | ||
|
||
"github.com/getsentry/sentry-go" | ||
) | ||
|
||
type Handler struct { | ||
repanic bool | ||
waitForDelivery bool | ||
timeout time.Duration | ||
} | ||
|
||
type Options struct { | ||
Repanic bool | ||
WaitForDelivery bool | ||
Timeout time.Duration | ||
} | ||
|
||
func New(options Options) *Handler { | ||
handler := Handler{ | ||
repanic: false, | ||
timeout: time.Second * 2, | ||
waitForDelivery: false, | ||
} | ||
|
||
if options.Repanic { | ||
handler.repanic = true | ||
} | ||
|
||
if options.WaitForDelivery { | ||
handler.waitForDelivery = true | ||
} | ||
|
||
return &handler | ||
} | ||
|
||
func (h *Handler) Handle() func(ctx iris.Context) { | ||
return func(ctx iris.Context) { | ||
r := ctx.Request() | ||
c := sentry.SetHubOnContext( | ||
context.WithValue(r.Context(), sentry.RequestContextKey, r), | ||
sentry.CurrentHub().Clone(), | ||
) | ||
defer h.recoverWithSentry(c, r) | ||
ctx.Next() | ||
} | ||
} | ||
|
||
func (h *Handler) recoverWithSentry(ctx context.Context, r *http.Request) { | ||
if err := recover(); err != nil { | ||
hub := sentry.GetHubFromContext(ctx) | ||
hub.ConfigureScope(func(scope *sentry.Scope) { | ||
scope.SetRequest(sentry.Request{}.FromHTTPRequest(r)) | ||
}) | ||
eventId := hub.RecoverWithContext(ctx, err) | ||
if eventId != nil && h.waitForDelivery { | ||
hub.Flush(h.timeout) | ||
} | ||
if h.repanic { | ||
panic(err) | ||
} | ||
} | ||
} |