errors.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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) error {
  72. return newOptionArgumentError("Invalid %s arguments: %s", name, args)
  73. }
  74. // newInvalidArgumentError creates a standardized error for an invalid single argument
  75. func newInvalidArgumentError(key, arg string, expected ...string) error {
  76. msg := "Invalid %s: %s"
  77. if len(expected) > 0 {
  78. msg += " (expected " + strings.Join(expected, ", ") + ")"
  79. }
  80. return newOptionArgumentError(msg, key, arg)
  81. }