errors.go 2.4 KB

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