report_error.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package state
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/types"
  8. "github.com/porter-dev/porter/internal/analytics"
  9. "github.com/porter-dev/porter/internal/models"
  10. "github.com/porter-dev/porter/provisioner/integrations/redis_stream"
  11. "github.com/porter-dev/porter/provisioner/server/config"
  12. ptypes "github.com/porter-dev/porter/provisioner/types"
  13. )
  14. type ReportErrorHandler struct {
  15. Config *config.Config
  16. decoderValidator shared.RequestDecoderValidator
  17. }
  18. func NewReportErrorHandler(
  19. config *config.Config,
  20. ) *ReportErrorHandler {
  21. return &ReportErrorHandler{
  22. Config: config,
  23. decoderValidator: shared.NewDefaultRequestDecoderValidator(config.Logger, config.Alerter),
  24. }
  25. }
  26. func (c *ReportErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. // read the infra from the attached scope
  28. infra, _ := r.Context().Value(types.InfraScope).(*models.Infra)
  29. operation, _ := r.Context().Value(types.OperationScope).(*models.Operation)
  30. req := &ptypes.ReportErrorRequest{}
  31. if ok := c.decoderValidator.DecodeAndValidate(w, r, req); !ok {
  32. return
  33. }
  34. // update the infra to indicate error
  35. infra.Status = "errored"
  36. infra, err := c.Config.Repo.Infra().UpdateInfra(infra)
  37. if err != nil {
  38. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  39. return
  40. }
  41. // update the operation with the error
  42. operation.Status = "errored"
  43. operation.Errored = true
  44. operation.Error = req.Error
  45. operation, err = c.Config.Repo.Infra().UpdateOperation(operation)
  46. if err != nil {
  47. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  48. return
  49. }
  50. // push to the operation stream
  51. err = redis_stream.SendOperationCompleted(c.Config.RedisClient, infra, operation)
  52. if err != nil {
  53. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  54. return
  55. }
  56. // push to the global stream
  57. err = redis_stream.PushToGlobalStream(c.Config.RedisClient, infra, operation, "error")
  58. if err != nil {
  59. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  60. return
  61. }
  62. // report the error to the error alerter but don't send to client
  63. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(
  64. fmt.Errorf(req.Error),
  65. ), false)
  66. switch infra.Kind {
  67. case types.InfraEKS, types.InfraDOKS, types.InfraGKE:
  68. var cluster *models.Cluster
  69. if cluster != nil {
  70. c.Config.AnalyticsClient.Track(analytics.ClusterProvisioningErrorTrack(
  71. &analytics.ClusterProvisioningErrorTrackOpts{
  72. ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(0, infra.ProjectID),
  73. ClusterType: infra.Kind,
  74. InfraID: infra.ID,
  75. },
  76. ))
  77. }
  78. }
  79. }