errors.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package processing
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/imgproxy/imgproxy/v3/ierrors"
  6. "github.com/imgproxy/imgproxy/v3/imagetype"
  7. )
  8. // Monitoring error categories
  9. const (
  10. categoryTimeout = "timeout"
  11. categoryImageDataSize = "image_data_size"
  12. categoryPathParsing = "path_parsing"
  13. categorySecurity = "security"
  14. categoryQueue = "queue"
  15. categoryDownload = "download"
  16. categoryProcessing = "processing"
  17. categoryIO = "IO"
  18. categoryConfig = "config(tmp)" // NOTE: THIS IS TEMPORARY
  19. )
  20. type (
  21. ResponseWriteError struct{ error }
  22. InvalidURLError string
  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. // newCantSaveError creates "resulting image not supported" error
  48. func newCantSaveError(format imagetype.Type) error {
  49. return ierrors.Wrap(newInvalidURLErrorf(
  50. http.StatusUnprocessableEntity,
  51. "Resulting image format is not supported: %s", format,
  52. ), 1, ierrors.WithCategory(categoryPathParsing))
  53. }
  54. // newCantLoadError creates "source image not supported" error
  55. func newCantLoadError(format imagetype.Type) error {
  56. return ierrors.Wrap(newInvalidURLErrorf(
  57. http.StatusUnprocessableEntity,
  58. "Source image format is not supported: %s", format,
  59. ), 1, ierrors.WithCategory(categoryProcessing))
  60. }