errors.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package handlers
  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. }