cobrasentry

package
v0.0.0-...-04ccfb0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ArgSentryDSN = &cobraviper.StringArgument{
	ArgumentName: "sentry-dsn",
	EnvName:      "SENTRY_DSN",
	Usage:        "The sentry DSN for reporting command error",
}
View Source
var RunEWrap = func(binderGetter BinderGetter, do InputRunEFunc) OutputRunEFunc {
	return func(cmd *cobra.Command, args []string) (err error) {
		ctx := context.Background()

		binder := binderGetter()
		sentryDSN := binder.GetString(cmd, ArgSentryDSN)

		var sentryHub *sentry.Hub
		if sentryDSN != "" {
			sentryHub, err = sentryutil.NewHub(sentryDSN)
			if err != nil {
				return err
			}
		}

		ctx = WithHub(ctx, sentryHub)
		loggerFactory := NewLoggerFactory(sentryHub)
		logger := loggerFactory.New("cobra-sentry").
			WithField("cmd_name", cmd.Name()).
			WithField("cmd_short", cmd.Short)

		defer func() {
			e := recover()
			if e != nil {
				err = panicutil.MakeError(e)
				logger.WithError(err).
					WithField("stack", errorutil.Callers(10000)).
					Error("panic occurred")
			}
			if sentryHub != nil {
				sentryHub.Flush(2 * time.Second)
			}
		}()

		err = do(ctx, cmd, args)
		if err != nil {
			logger.WithError(err).Error("command exit")
		}
		return err
	}
}

Functions

func GetHub

func GetHub(ctx context.Context) *sentry.Hub

func NewLoggerFactory

func NewLoggerFactory(sentryHub *sentry.Hub) *log.Factory

func WithHub

func WithHub(ctx context.Context, hub *sentry.Hub) context.Context

Types

type BinderGetter

type BinderGetter func() *cobraviper.Binder

type InputRunEFunc

type InputRunEFunc func(ctx context.Context, cmd *cobra.Command, args []string) error

type OutputRunEFunc

type OutputRunEFunc func(cmd *cobra.Command, args []string) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL