errors.go 1.6 KB

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