errors.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package apierrors
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "github.com/porter-dev/porter/api/server/shared/config"
  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. config *config.Config,
  78. w http.ResponseWriter,
  79. r *http.Request,
  80. err RequestError,
  81. writeErr bool,
  82. opts ...ErrorOpts,
  83. ) {
  84. extErrorStr := err.ExternalError()
  85. // log the internal error
  86. event := config.Logger.Warn().
  87. Str("internal_error", err.InternalError()).
  88. Str("external_error", extErrorStr)
  89. data := logger.AddLoggingContextScopes(r.Context(), event)
  90. logger.AddLoggingRequestMeta(r, event)
  91. event.Send()
  92. // if the status code is internal server error, use alerter
  93. if err.GetStatusCode() == http.StatusInternalServerError && config.Alerter != nil {
  94. data["method"] = r.Method
  95. data["url"] = r.URL.String()
  96. config.Alerter.SendAlert(r.Context(), err, data)
  97. }
  98. if writeErr {
  99. // send the external error
  100. resp := &types.ExternalError{
  101. Error: extErrorStr,
  102. }
  103. if len(opts) > 0 {
  104. resp.Code = opts[0].Code
  105. }
  106. // write the status code
  107. w.WriteHeader(err.GetStatusCode())
  108. writerErr := json.NewEncoder(w).Encode(resp)
  109. if writerErr != nil {
  110. event := config.Logger.Error().
  111. Err(writerErr)
  112. logger.AddLoggingContextScopes(r.Context(), event)
  113. logger.AddLoggingRequestMeta(r, event)
  114. event.Send()
  115. }
  116. }
  117. return
  118. }