No Description

ErrorResponse.php 890B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace MailPoet\API\JSON;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\WP\Functions as WPFunctions;
  5. class ErrorResponse extends Response {
  6. public $errors;
  7. public function __construct(
  8. $errors = [],
  9. $meta = [],
  10. $status = self::STATUS_NOT_FOUND
  11. ) {
  12. parent::__construct($status, $meta);
  13. $this->errors = $this->formatErrors($errors);
  14. }
  15. public function getData() {
  16. return (empty($this->errors)) ? null : ['errors' => $this->errors];
  17. }
  18. public function formatErrors($errors = []) {
  19. return array_map(function($error, $message) {
  20. // sanitize SQL error
  21. if (preg_match('/^SQLSTATE/i', $message)) {
  22. $message = WPFunctions::get()->__('An unknown error occurred.', 'mailpoet');
  23. }
  24. return [
  25. 'error' => $error,
  26. 'message' => $message,
  27. ];
  28. }, array_keys($errors), array_values($errors));
  29. }
  30. }