Няма описание

exceptions.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php declare(strict_types = 1);
  2. // phpcs:ignoreFile PSR1.Classes.ClassDeclaration
  3. namespace MailPoet;
  4. if (!defined('ABSPATH')) exit;
  5. /**
  6. * Provides information for converting exceptions to HTTP responses.
  7. */
  8. interface HttpAwareException {
  9. public function getHttpStatusCode(): int;
  10. }
  11. /**
  12. * Frames all MailPoet exceptions ("$e instanceof MailPoet\Exception").
  13. */
  14. abstract class Exception extends \Exception {
  15. /** @var string[] */
  16. private $errors = [];
  17. final public function __construct($message = '', $code = 0, \Throwable $previous = null) {
  18. parent::__construct($message, $code, $previous);
  19. }
  20. public static function create(\Throwable $previous = null): self {
  21. return new static('', 0, $previous);
  22. }
  23. public function withMessage(string $message): self {
  24. $this->message = $message;
  25. return $this;
  26. }
  27. public function withCode(int $code): self {
  28. $this->code = $code;
  29. return $this;
  30. }
  31. public function withErrors(array $errors): self {
  32. $this->errors = $errors;
  33. return $this;
  34. }
  35. public function getErrors(): array {
  36. return $this->errors;
  37. }
  38. }
  39. /**
  40. * USE: Generic runtime error. When possible, use a more specific exception instead.
  41. * API: 500 Server Error (not HTTP-aware)
  42. */
  43. class RuntimeException extends Exception {}
  44. /**
  45. * USE: When wrong data VALUE is received.
  46. * API: 400 Bad Request
  47. */
  48. class UnexpectedValueException extends RuntimeException implements HttpAwareException {
  49. public function getHttpStatusCode(): int {
  50. return 400;
  51. }
  52. }
  53. /**
  54. * USE: When an action is forbidden for given actor (although generally valid).
  55. * API: 403 Forbidden
  56. */
  57. class AccessDeniedException extends UnexpectedValueException implements HttpAwareException {
  58. public function getHttpStatusCode(): int {
  59. return 403;
  60. }
  61. }
  62. /**
  63. * USE: When the main resource we're interested in doesn't exist.
  64. * API: 404 Not Found
  65. */
  66. class NotFoundException extends UnexpectedValueException implements HttpAwareException {
  67. public function getHttpStatusCode(): int {
  68. return 404;
  69. }
  70. }
  71. /**
  72. * USE: When the main action produces conflict (i.e. duplicate key).
  73. * API: 409 Conflict
  74. */
  75. class ConflictException extends UnexpectedValueException implements HttpAwareException {
  76. public function getHttpStatusCode(): int {
  77. return 409;
  78. }
  79. }
  80. /**
  81. * USE: An application state that should not occur. Can be subclassed for feature-specific exceptions.
  82. * API: 500 Server Error (not HTTP-aware)
  83. */
  84. class InvalidStateException extends RuntimeException {}