ResponseBuilders
2 weeks 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
ErrorHandler.php
26 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\API\JSON; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Exception; |
| 9 | use MailPoet\HttpAwareException; |
| 10 | |
| 11 | class ErrorHandler { |
| 12 | /** @var string[] */ |
| 13 | private $defaultErrors = []; |
| 14 | |
| 15 | public function convertToResponse(\Throwable $e): ErrorResponse { |
| 16 | $this->defaultErrors[Error::UNKNOWN] = __('An unknown error occurred.', 'mailpoet'); |
| 17 | |
| 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 | } |
| 26 |