errors.go 2.9 KB

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