errors.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package server
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "github.com/imgproxy/imgproxy/v3/ierrors"
  7. )
  8. type (
  9. RouteNotDefinedError string
  10. RequestCancelledError string
  11. RequestTimeoutError string
  12. InvalidSecretError struct{}
  13. )
  14. func newRouteNotDefinedError(path string) *ierrors.Error {
  15. return ierrors.Wrap(
  16. RouteNotDefinedError(fmt.Sprintf("Route for %s is not defined", path)),
  17. 1,
  18. ierrors.WithStatusCode(http.StatusNotFound),
  19. ierrors.WithPublicMessage("Not found"),
  20. ierrors.WithShouldReport(false),
  21. )
  22. }
  23. func (e RouteNotDefinedError) Error() string { return string(e) }
  24. func newRequestCancelledError(after time.Duration) *ierrors.Error {
  25. return ierrors.Wrap(
  26. RequestCancelledError(fmt.Sprintf("Request was cancelled after %v", after)),
  27. 1,
  28. ierrors.WithStatusCode(499),
  29. ierrors.WithPublicMessage("Cancelled"),
  30. ierrors.WithShouldReport(false),
  31. )
  32. }
  33. func (e RequestCancelledError) Error() string { return string(e) }
  34. func newRequestTimeoutError(after time.Duration) *ierrors.Error {
  35. return ierrors.Wrap(
  36. RequestTimeoutError(fmt.Sprintf("Request was timed out after %v", after)),
  37. 1,
  38. ierrors.WithStatusCode(http.StatusServiceUnavailable),
  39. ierrors.WithPublicMessage("Gateway Timeout"),
  40. ierrors.WithShouldReport(false),
  41. )
  42. }
  43. func (e RequestTimeoutError) Error() string { return string(e) }
  44. func newInvalidSecretError() error {
  45. return ierrors.Wrap(
  46. InvalidSecretError{},
  47. 1,
  48. ierrors.WithStatusCode(http.StatusForbidden),
  49. ierrors.WithPublicMessage("Forbidden"),
  50. ierrors.WithShouldReport(false),
  51. )
  52. }
  53. func (e InvalidSecretError) Error() string { return "Invalid secret" }