errors.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package options
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "github.com/imgproxy/imgproxy/v3/ierrors"
  7. )
  8. type (
  9. InvalidURLError string
  10. UnknownOptionError string
  11. OptionArgumentError string
  12. SecurityOptionsError struct{}
  13. )
  14. func newInvalidURLError(format string, args ...interface{}) error {
  15. return ierrors.Wrap(
  16. InvalidURLError(fmt.Sprintf(format, args...)),
  17. 1,
  18. ierrors.WithStatusCode(http.StatusNotFound),
  19. ierrors.WithPublicMessage("Invalid URL"),
  20. ierrors.WithShouldReport(false),
  21. )
  22. }
  23. func (e InvalidURLError) Error() string { return string(e) }
  24. func newUnknownOptionError(kind, opt string) error {
  25. return ierrors.Wrap(
  26. UnknownOptionError(fmt.Sprintf("Unknown %s option %s", kind, opt)),
  27. 1,
  28. ierrors.WithStatusCode(http.StatusNotFound),
  29. ierrors.WithPublicMessage("Invalid URL"),
  30. ierrors.WithShouldReport(false),
  31. )
  32. }
  33. func newForbiddenOptionError(kind, opt string) error {
  34. return ierrors.Wrap(
  35. UnknownOptionError(fmt.Sprintf("Forbidden %s option %s", kind, opt)),
  36. 1,
  37. ierrors.WithStatusCode(http.StatusNotFound),
  38. ierrors.WithPublicMessage("Invalid URL"),
  39. ierrors.WithShouldReport(false),
  40. )
  41. }
  42. func (e UnknownOptionError) Error() string { return string(e) }
  43. func newOptionArgumentError(format string, args ...interface{}) error {
  44. return ierrors.Wrap(
  45. OptionArgumentError(fmt.Sprintf(format, args...)),
  46. 1,
  47. ierrors.WithStatusCode(http.StatusNotFound),
  48. ierrors.WithPublicMessage("Invalid URL"),
  49. ierrors.WithShouldReport(false),
  50. )
  51. }
  52. func (e OptionArgumentError) Error() string { return string(e) }
  53. func newSecurityOptionsError() error {
  54. return ierrors.Wrap(
  55. SecurityOptionsError{},
  56. 1,
  57. ierrors.WithStatusCode(http.StatusForbidden),
  58. ierrors.WithPublicMessage("Invalid URL"),
  59. ierrors.WithShouldReport(false),
  60. )
  61. }
  62. func (e SecurityOptionsError) Error() string { return "Security processing options are not allowed" }
  63. // newInvalidArgsError creates a standardized error for invalid arguments
  64. func newInvalidArgsError(name string, args []string, expected ...string) error {
  65. msg := "Invalid %s arguments: %s"
  66. if len(expected) > 0 {
  67. msg += " (expected " + strings.Join(expected, ", ") + ")"
  68. }
  69. return newOptionArgumentError(msg, name, args)
  70. }