Files
piratepoet/lib/API/JSON/ErrorHandler.php
Pavel Dohnal cdda3480ca Make all constructor signatures multiline
[MAILPOET-3732]
2021-09-16 14:19:40 +02:00

30 lines
829 B
PHP

<?php declare(strict_types = 1);
namespace MailPoet\API\JSON;
use MailPoet\Exception;
use MailPoet\HttpAwareException;
use MailPoet\WP\Functions as WPFunctions;
class ErrorHandler {
/** @var string[] */
private $defaultErrors;
public function __construct(
WPFunctions $wp
) {
$this->defaultErrors = [
Error::UNKNOWN => $wp->__('An unknown error occurred.', 'mailpoet'),
];
}
public function convertToResponse(\Throwable $e): ErrorResponse {
if ($e instanceof Exception) {
$errors = $e->getErrors() ?: $this->defaultErrors;
$statusCode = $e instanceof HttpAwareException ? $e->getHttpStatusCode() : Response::STATUS_UNKNOWN;
return new ErrorResponse($errors, [], $statusCode);
}
return new ErrorResponse($this->defaultErrors, [], Response::STATUS_UNKNOWN);
}
}