error_handler.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package errors
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/fatih/color"
  6. "github.com/getsentry/sentry-go"
  7. "github.com/porter-dev/porter/cli/cmd/config"
  8. )
  9. var SentryDSN string = ""
  10. type errorHandler interface {
  11. HandleError(error)
  12. }
  13. type standardErrorHandler struct{}
  14. func (h *standardErrorHandler) HandleError(err error) {
  15. color.New(color.FgRed).Fprintf(os.Stderr, "error: %s\n", err.Error())
  16. }
  17. type sentryErrorHandler struct{}
  18. func (h *sentryErrorHandler) HandleError(err error) {
  19. if SentryDSN != "" {
  20. localHub := sentry.CurrentHub().Clone()
  21. localHub.ConfigureScope(func(scope *sentry.Scope) {
  22. scope.SetTags(map[string]string{
  23. "host": config.GetCLIConfig().Host,
  24. "project": fmt.Sprintf("%d", config.GetCLIConfig().Project),
  25. "cluster": fmt.Sprintf("%d", config.GetCLIConfig().Cluster),
  26. })
  27. })
  28. if eventID := localHub.CaptureException(err); eventID == nil {
  29. color.New(color.FgRed).Fprintf(os.Stderr, "error in sending exception to sentry\n")
  30. }
  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. }