ResponseBuilders
1 month ago
v1
1 month ago
API.php
2 months ago
Endpoint.php
1 year ago
Error.php
2 months ago
ErrorHandler.php
1 year ago
ErrorResponse.php
3 years ago
Response.php
2 months ago
SuccessResponse.php
3 years ago
index.php
3 years ago
ErrorResponse.php
37 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\API\JSON; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | class ErrorResponse extends Response { |
| 9 | public $errors; |
| 10 | |
| 11 | public function __construct( |
| 12 | $errors = [], |
| 13 | $meta = [], |
| 14 | $status = self::STATUS_NOT_FOUND |
| 15 | ) { |
| 16 | parent::__construct($status, $meta); |
| 17 | $this->errors = $this->formatErrors($errors); |
| 18 | } |
| 19 | |
| 20 | public function getData() { |
| 21 | return (empty($this->errors)) ? null : ['errors' => $this->errors]; |
| 22 | } |
| 23 | |
| 24 | public function formatErrors($errors = []) { |
| 25 | return array_map(function($error, $message) { |
| 26 | // sanitize SQL error |
| 27 | if (preg_match('/^SQLSTATE/i', $message)) { |
| 28 | $message = __('An unknown error occurred.', 'mailpoet'); |
| 29 | } |
| 30 | return [ |
| 31 | 'error' => $error, |
| 32 | 'message' => $message, |
| 33 | ]; |
| 34 | }, array_keys($errors), array_values($errors)); |
| 35 | } |
| 36 | } |
| 37 |