fix: enable sentry when configured

This commit is contained in:
Paul Pan 2023-12-20 23:45:53 +08:00
parent 085202c174
commit d8df6d577e
Signed by: Paul
GPG Key ID: D639BDF5BA578AF4
2 changed files with 11 additions and 5 deletions

View File

@ -94,10 +94,13 @@ func prepareServices(c *cli.Context) *do.Injector {
func wrap(f func(i *do.Injector) error) func(*cli.Context) error { func wrap(f func(i *do.Injector) error) func(*cli.Context) error {
return func(c *cli.Context) error { return func(c *cli.Context) error {
defer func() { defer func() {
if r := recover(); r != nil { if cmd.SentryDSN != "" {
sentry.CaptureException(r.(error)) // only recover when sentry is enabled
sentry.Flush(time.Second * 2) if r := recover(); r != nil {
slog.Printf("Panic Captured: %v", r) sentry.CaptureException(r.(error))
sentry.Flush(time.Second * 2)
slog.Printf("Panic Captured: %v", r)
}
} }
}() }()

View File

@ -1,6 +1,7 @@
package log package log
import ( import (
"git.0x7f.app/WOJ/woj-server/cmd"
"git.0x7f.app/WOJ/woj-server/internal/misc/config" "git.0x7f.app/WOJ/woj-server/internal/misc/config"
"git.0x7f.app/WOJ/woj-server/pkg/utils" "git.0x7f.app/WOJ/woj-server/pkg/utils"
"github.com/TheZeroSlave/zapsentry" "github.com/TheZeroSlave/zapsentry"
@ -48,7 +49,9 @@ func NewService(i *do.Injector) (Service, error) {
return nil, err return nil, err
} }
srv.logger = attachSentry(srv.logger) if cmd.SentryDSN != "" {
srv.logger = attachSentry(srv.logger)
}
return srv, nil return srv, nil
} }