2
0

errors.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package apierrors
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors/alerter"
  7. "github.com/porter-dev/porter/api/types"
  8. "github.com/porter-dev/porter/pkg/logger"
  9. )
  10. type RequestError interface {
  11. Error() string
  12. ExternalError() string
  13. InternalError() string
  14. GetStatusCode() int
  15. }
  16. type ErrInternal struct {
  17. err error
  18. }
  19. func NewErrInternal(err error) RequestError {
  20. return &ErrInternal{err}
  21. }
  22. func (e *ErrInternal) Error() string {
  23. return e.err.Error()
  24. }
  25. func (e *ErrInternal) InternalError() string {
  26. return e.err.Error()
  27. }
  28. func (e *ErrInternal) ExternalError() string {
  29. return "An internal error occurred."
  30. }
  31. func (e *ErrInternal) GetStatusCode() int {
  32. return http.StatusInternalServerError
  33. }
  34. type ErrForbidden struct {
  35. err error
  36. }
  37. func NewErrForbidden(err error) RequestError {
  38. return &ErrForbidden{err}
  39. }
  40. func (e *ErrForbidden) Error() string {
  41. return e.err.Error()
  42. }
  43. func (e *ErrForbidden) InternalError() string {
  44. return e.err.Error()
  45. }
  46. func (e *ErrForbidden) ExternalError() string {
  47. return "Forbidden"
  48. }
  49. func (e *ErrForbidden) GetStatusCode() int {
  50. return http.StatusForbidden
  51. }
  52. // errors that should be passed directly, with no filter
  53. type ErrPassThroughToClient struct {
  54. err error
  55. statusCode int
  56. errDetails []string
  57. }
  58. func NewErrPassThroughToClient(err error, statusCode int, details ...string) RequestError {
  59. return &ErrPassThroughToClient{err, statusCode, details}
  60. }
  61. func (e *ErrPassThroughToClient) Error() string {
  62. return e.err.Error()
  63. }
  64. func (e *ErrPassThroughToClient) InternalError() string {
  65. return e.err.Error() + strings.Join(e.errDetails, ",")
  66. }
  67. func (e *ErrPassThroughToClient) ExternalError() string {
  68. return e.err.Error()
  69. }
  70. func (e *ErrPassThroughToClient) GetStatusCode() int {
  71. return e.statusCode
  72. }
  73. // errors that denote that a resource was not found
  74. type ErrNotFound struct {
  75. err error
  76. }
  77. func NewErrNotFound(err error) RequestError {
  78. return &ErrNotFound{err}
  79. }
  80. func (e *ErrNotFound) Error() string {
  81. return e.err.Error()
  82. }
  83. func (e *ErrNotFound) InternalError() string {
  84. return e.err.Error()
  85. }
  86. func (e *ErrNotFound) ExternalError() string {
  87. return "Resource not found."
  88. }
  89. func (e *ErrNotFound) GetStatusCode() int {
  90. return http.StatusNotFound
  91. }
  92. type ErrorOpts struct {
  93. Code uint
  94. }
  95. func HandleAPIError(
  96. l *logger.Logger,
  97. al alerter.Alerter,
  98. w http.ResponseWriter,
  99. r *http.Request,
  100. err RequestError,
  101. writeErr bool,
  102. opts ...ErrorOpts,
  103. ) {
  104. extErrorStr := err.ExternalError()
  105. // log the internal error
  106. event := l.Warn().
  107. Str("internal_error", err.InternalError()).
  108. Str("external_error", extErrorStr)
  109. data := logger.AddLoggingContextScopes(r.Context(), event)
  110. logger.AddLoggingRequestMeta(r, event)
  111. event.Send()
  112. // if the status code is internal server error, use alerter
  113. if err.GetStatusCode() == http.StatusInternalServerError && al != nil {
  114. data["method"] = r.Method
  115. data["url"] = r.URL.String()
  116. eventID := al.SendAlert(r.Context(), err, data)
  117. if _, ok := al.(*alerter.SentryAlerter); ok && eventID != nil {
  118. data["sentry_event_id"] = *eventID
  119. }
  120. }
  121. if writeErr {
  122. // send the external error
  123. resp := &types.ExternalError{
  124. Error: extErrorStr,
  125. }
  126. if len(opts) > 0 {
  127. resp.Code = opts[0].Code
  128. }
  129. // write the status code
  130. w.WriteHeader(err.GetStatusCode())
  131. writerErr := json.NewEncoder(w).Encode(resp)
  132. if writerErr != nil {
  133. event := l.Error().
  134. Err(writerErr)
  135. logger.AddLoggingContextScopes(r.Context(), event)
  136. logger.AddLoggingRequestMeta(r, event)
  137. event.Send()
  138. }
  139. }
  140. }