| 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 |
|