errors.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/internal/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. type ErrorOpts struct {
  74. Code uint
  75. }
  76. func HandleAPIError(
  77. l *logger.Logger,
  78. alerter alerter.Alerter,
  79. w http.ResponseWriter,
  80. r *http.Request,
  81. err RequestError,
  82. writeErr bool,
  83. opts ...ErrorOpts,
  84. ) {
  85. extErrorStr := err.ExternalError()
  86. // log the internal error
  87. event := l.Warn().
  88. Str("internal_error", err.InternalError()).
  89. Str("external_error", extErrorStr)
  90. data := logger.AddLoggingContextScopes(r.Context(), event)
  91. logger.AddLoggingRequestMeta(r, event)
  92. event.Send()
  93. // if the status code is internal server error, use alerter
  94. if err.GetStatusCode() == http.StatusInternalServerError && alerter != nil {
  95. data["method"] = r.Method
  96. data["url"] = r.URL.String()
  97. alerter.SendAlert(r.Context(), err, data)
  98. }
  99. if writeErr {
  100. // send the external error
  101. resp := &types.ExternalError{
  102. Error: extErrorStr,
  103. }
  104. if len(opts) > 0 {
  105. resp.Code = opts[0].Code
  106. }
  107. // write the status code
  108. w.WriteHeader(err.GetStatusCode())
  109. writerErr := json.NewEncoder(w).Encode(resp)
  110. if writerErr != nil {
  111. event := l.Error().
  112. Err(writerErr)
  113. logger.AddLoggingContextScopes(r.Context(), event)
  114. logger.AddLoggingRequestMeta(r, event)
  115. event.Send()
  116. }
  117. }
  118. return
  119. }