1
0

errors.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package optionsparser
  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) error {
  65. return newOptionArgumentError("Invalid %s arguments: %s", name, args)
  66. }
  67. // newInvalidArgumentError creates a standardized error for an invalid single argument
  68. func newInvalidArgumentError(key, arg string, expected ...string) error {
  69. msg := "Invalid %s: %s"
  70. if len(expected) > 0 {
  71. msg += " (expected " + strings.Join(expected, ", ") + ")"
  72. }
  73. return newOptionArgumentError(msg, key, arg)
  74. }