1
0

errors.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package server
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/imgproxy/imgproxy/v3/ierrors"
  8. )
  9. type (
  10. RouteNotDefinedError string
  11. RequestCancelledError string
  12. RequestTimeoutError string
  13. InvalidSecretError struct{}
  14. )
  15. func newRouteNotDefinedError(path string) *ierrors.Error {
  16. return ierrors.Wrap(
  17. RouteNotDefinedError(fmt.Sprintf("Route for %s is not defined", path)),
  18. 1,
  19. ierrors.WithStatusCode(http.StatusNotFound),
  20. ierrors.WithPublicMessage("Not found"),
  21. ierrors.WithShouldReport(false),
  22. )
  23. }
  24. func (e RouteNotDefinedError) Error() string { return string(e) }
  25. func newRequestCancelledError(after time.Duration) *ierrors.Error {
  26. return ierrors.Wrap(
  27. RequestCancelledError(fmt.Sprintf("Request was cancelled after %v", after)),
  28. 1,
  29. ierrors.WithStatusCode(499),
  30. ierrors.WithPublicMessage("Cancelled"),
  31. ierrors.WithShouldReport(false),
  32. ierrors.WithCategory(categoryTimeout),
  33. )
  34. }
  35. func (e RequestCancelledError) Error() string { return string(e) }
  36. func (e RequestCancelledError) Unwrap() error {
  37. return context.Canceled
  38. }
  39. func newRequestTimeoutError(after time.Duration) *ierrors.Error {
  40. return ierrors.Wrap(
  41. RequestTimeoutError(fmt.Sprintf("Request was timed out after %v", after)),
  42. 1,
  43. ierrors.WithStatusCode(http.StatusServiceUnavailable),
  44. ierrors.WithPublicMessage("Gateway Timeout"),
  45. ierrors.WithShouldReport(false),
  46. ierrors.WithCategory(categoryTimeout),
  47. )
  48. }
  49. func (e RequestTimeoutError) Error() string { return string(e) }
  50. func (e RequestTimeoutError) Unwrap() error {
  51. return context.DeadlineExceeded
  52. }
  53. func newInvalidSecretError() error {
  54. return ierrors.Wrap(
  55. InvalidSecretError{},
  56. 1,
  57. ierrors.WithStatusCode(http.StatusForbidden),
  58. ierrors.WithPublicMessage("Forbidden"),
  59. ierrors.WithShouldReport(false),
  60. )
  61. }
  62. func (e InvalidSecretError) Error() string { return "Invalid secret" }