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