1
0
mirror of https://github.com/flarum/core.git synced 2025-08-08 01:16:52 +02:00

API Client: Use new error handling mechanism

This commit is contained in:
Franz Liedke
2019-08-09 23:57:33 +02:00
parent 57ce25301d
commit 81a8736ba9
10 changed files with 33 additions and 68 deletions

View File

@@ -12,11 +12,14 @@
namespace Flarum\Api;
use Exception;
use Flarum\Foundation\ErrorHandling\JsonApiRenderer;
use Flarum\Foundation\ErrorHandling\Registry;
use Flarum\User\User;
use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;
use Zend\Diactoros\ServerRequestFactory;
class Client
@@ -27,18 +30,18 @@ class Client
protected $container;
/**
* @var ErrorHandler
* @var Registry
*/
protected $errorHandler;
protected $registry;
/**
* @param Container $container
* @param ErrorHandler $errorHandler
* @param Registry $registry
*/
public function __construct(Container $container, ErrorHandler $errorHandler = null)
public function __construct(Container $container, Registry $registry)
{
$this->container = $container;
$this->errorHandler = $errorHandler;
$this->registry = $registry;
}
/**
@@ -69,23 +72,14 @@ class Client
try {
return $controller->handle($request);
} catch (Exception $e) {
if (! $this->errorHandler) {
} catch (Throwable $e) {
$error = $this->registry->handle($e);
if ($error->shouldBeReported()) {
throw $e;
}
return $this->errorHandler->handle($e);
return (new JsonApiRenderer)->format($error, $request);
}
}
/**
* @param ErrorHandler $errorHandler
* @return Client
*/
public function setErrorHandler(?ErrorHandler $errorHandler): self
{
$this->errorHandler = $errorHandler;
return $this;
}
}

View File

@@ -70,8 +70,6 @@ class Frontend
{
$actor = $request->getAttribute('actor');
$this->api->setErrorHandler(null);
return $this->getResponseBody(
$this->api->send(ShowForumController::class, $actor)
);