errors.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/imgproxy/imgproxy/v3/ierrors"
  6. )
  7. type (
  8. ResponseWriteError struct{ error }
  9. InvalidURLError string
  10. TooManyRequestsError struct{}
  11. InvalidSecretError struct{}
  12. )
  13. // Error categories which are used in metrics
  14. // NOTE: possibly -> metrics (?)
  15. const (
  16. categoryTimeout = "timeout"
  17. categoryImageDataSize = "image_data_size"
  18. categoryPathParsing = "path_parsing"
  19. categorySecurity = "security"
  20. categoryQueue = "queue"
  21. categoryDownload = "download"
  22. categoryProcessing = "processing"
  23. categoryIO = "IO"
  24. categoryStreaming = "streaming"
  25. )
  26. func newResponseWriteError(cause error) *ierrors.Error {
  27. return ierrors.Wrap(
  28. ResponseWriteError{cause},
  29. 1,
  30. ierrors.WithPublicMessage("Failed to write response"),
  31. )
  32. }
  33. func (e ResponseWriteError) Error() string {
  34. return fmt.Sprintf("Failed to write response: %s", e.error)
  35. }
  36. func (e ResponseWriteError) Unwrap() error {
  37. return e.error
  38. }
  39. func newInvalidURLErrorf(status int, format string, args ...interface{}) error {
  40. return ierrors.Wrap(
  41. InvalidURLError(fmt.Sprintf(format, args...)),
  42. 1,
  43. ierrors.WithStatusCode(status),
  44. ierrors.WithPublicMessage("Invalid URL"),
  45. ierrors.WithShouldReport(false),
  46. )
  47. }
  48. func (e InvalidURLError) Error() string { return string(e) }
  49. func newTooManyRequestsError() error {
  50. return ierrors.Wrap(
  51. TooManyRequestsError{},
  52. 1,
  53. ierrors.WithStatusCode(http.StatusTooManyRequests),
  54. ierrors.WithPublicMessage("Too many requests"),
  55. ierrors.WithShouldReport(false),
  56. )
  57. }
  58. func (e TooManyRequestsError) Error() string { return "Too many requests" }