errors.go 926 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package ierrors
  2. import (
  3. "github.com/imgproxy/imgproxy/v3/errwrap"
  4. )
  5. type Error = errwrap.ErrWrap
  6. type Option func(*Error) *Error
  7. // Now, it's fallback to the original Wrap function
  8. func Wrap(err error, stackSkip int, opts ...Option) *Error {
  9. if err == nil {
  10. return nil
  11. }
  12. var e *Error
  13. x, ok := err.(*Error)
  14. if ok {
  15. e = errwrap.Wrap(x)
  16. } else {
  17. e = errwrap.From(err, stackSkip)
  18. }
  19. for _, opt := range opts {
  20. e = opt(e)
  21. }
  22. return e
  23. }
  24. func WithStatusCode(code int) Option {
  25. return func(e *Error) *Error {
  26. x := e.WithStatusCode(code)
  27. return x
  28. }
  29. }
  30. func WithPublicMessage(msg string) Option {
  31. return func(e *Error) *Error {
  32. return e.WithPublicMessage(msg)
  33. }
  34. }
  35. func WithPrefix(prefix string) Option {
  36. return func(e *Error) *Error {
  37. return errwrap.Wrapf(e, "%s", prefix)
  38. }
  39. }
  40. func WithShouldReport(report bool) Option {
  41. return func(e *Error) *Error {
  42. return e.WithShouldReport(report)
  43. }
  44. }