Nenhuma Descrição

ErrorHandler.php 860B

123456789101112131415161718192021222324252627282930313233
  1. <?php declare(strict_types = 1);
  2. namespace MailPoet\API\JSON;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Exception;
  5. use MailPoet\HttpAwareException;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. class ErrorHandler {
  8. /** @var string[] */
  9. private $defaultErrors;
  10. public function __construct(
  11. WPFunctions $wp
  12. ) {
  13. $this->defaultErrors = [
  14. Error::UNKNOWN => $wp->__('An unknown error occurred.', 'mailpoet'),
  15. ];
  16. }
  17. public function convertToResponse(\Throwable $e): ErrorResponse {
  18. if ($e instanceof Exception) {
  19. $errors = $e->getErrors() ?: $this->defaultErrors;
  20. $statusCode = $e instanceof HttpAwareException ? $e->getHttpStatusCode() : Response::STATUS_UNKNOWN;
  21. return new ErrorResponse($errors, [], $statusCode);
  22. }
  23. return new ErrorResponse($this->defaultErrors, [], Response::STATUS_UNKNOWN);
  24. }
  25. }