errors.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package apierrors
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "github.com/porter-dev/porter/api/server/shared/config"
  7. "github.com/porter-dev/porter/api/types"
  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. func HandleAPIError(
  72. ctx context.Context,
  73. config *config.Config,
  74. w http.ResponseWriter,
  75. err RequestError,
  76. ) {
  77. // if the status code is internal server error, use alerter
  78. if err.GetStatusCode() == http.StatusInternalServerError && config.Alerter != nil {
  79. config.Alerter.SendAlert(ctx, err)
  80. }
  81. extErrorStr := err.ExternalError()
  82. // log the internal error
  83. config.Logger.Warn().
  84. Str("internal_error", err.InternalError()).
  85. Str("external_error", extErrorStr).
  86. Msg("")
  87. // send the external error
  88. resp := &types.ExternalError{
  89. Error: extErrorStr,
  90. }
  91. // write the status code
  92. w.WriteHeader(err.GetStatusCode())
  93. writerErr := json.NewEncoder(w).Encode(resp)
  94. if writerErr != nil {
  95. config.Logger.Error().
  96. Err(writerErr).
  97. Msg("")
  98. }
  99. return
  100. }