error_handler.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package errors
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/fatih/color"
  7. "github.com/getsentry/sentry-go"
  8. "github.com/porter-dev/porter/cli/cmd/config"
  9. )
  10. var SentryDSN string = ""
  11. type errorHandler interface {
  12. HandleError(error)
  13. }
  14. type standardErrorHandler struct{}
  15. func (h *standardErrorHandler) HandleError(err error) {
  16. color.New(color.FgRed).Fprintf(os.Stderr, "error: %s\n", err.Error())
  17. }
  18. type sentryErrorHandler struct{}
  19. func (h *sentryErrorHandler) HandleError(err error) {
  20. if SentryDSN != "" {
  21. localHub := sentry.CurrentHub().Clone()
  22. localHub.ConfigureScope(func(scope *sentry.Scope) {
  23. scope.SetTags(map[string]string{
  24. "host": config.GetCLIConfig().Host,
  25. "project": fmt.Sprintf("%d", config.GetCLIConfig().Project),
  26. "cluster": fmt.Sprintf("%d", config.GetCLIConfig().Cluster),
  27. })
  28. })
  29. localHub.CaptureException(err)
  30. sentry.Flush(2 * time.Second)
  31. }
  32. color.New(color.FgRed).Fprintf(os.Stderr, "error: %s\n", err.Error())
  33. }
  34. func GetErrorHandler() errorHandler {
  35. if SentryDSN != "" {
  36. return &sentryErrorHandler{}
  37. }
  38. return &standardErrorHandler{}
  39. }