Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new error handling stack #1843

Merged
merged 7 commits into from Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Admin/AdminServiceProvider.php
Expand Up @@ -16,6 +16,10 @@
use Flarum\Extension\Event\Enabled;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Application;
use Flarum\Foundation\ErrorHandling\Registry;
use Flarum\Foundation\ErrorHandling\Reporter;
use Flarum\Foundation\ErrorHandling\ViewRenderer;
use Flarum\Foundation\ErrorHandling\WhoopsRenderer;
use Flarum\Foundation\Event\ClearingCache;
use Flarum\Frontend\AddLocaleAssets;
use Flarum\Frontend\AddTranslations;
Expand Down Expand Up @@ -51,11 +55,11 @@ public function register()
$pipe = new MiddlewarePipe;

// All requests should first be piped through our global error handler
if ($app->inDebugMode()) {
$pipe->pipe($app->make(HttpMiddleware\HandleErrorsWithWhoops::class));
} else {
$pipe->pipe($app->make(HttpMiddleware\HandleErrorsWithView::class));
}
$pipe->pipe(new HttpMiddleware\HandleErrors(
$app->make(Registry::class),
$app->inDebugMode() ? $app->make(WhoopsRenderer::class) : $app->make(ViewRenderer::class),
$app->tagged(Reporter::class)
));

$pipe->pipe($app->make(HttpMiddleware\ParseJsonBody::class));
$pipe->pipe($app->make(HttpMiddleware\StartSession::class));
Expand Down
30 changes: 8 additions & 22 deletions src/Api/ApiServiceProvider.php
Expand Up @@ -20,12 +20,13 @@
use Flarum\Event\ConfigureNotificationTypes;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Application;
use Flarum\Foundation\ErrorHandling\JsonApiRenderer;
use Flarum\Foundation\ErrorHandling\Registry;
use Flarum\Foundation\ErrorHandling\Reporter;
use Flarum\Http\Middleware as HttpMiddleware;
use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory;
use Flarum\Http\UrlGenerator;
use Tobscure\JsonApi\ErrorHandler;
use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler;
use Zend\Stratigility\MiddlewarePipe;

class ApiServiceProvider extends AbstractServiceProvider
Expand All @@ -49,7 +50,11 @@ public function register()
$this->app->singleton('flarum.api.middleware', function (Application $app) {
$pipe = new MiddlewarePipe;

$pipe->pipe($app->make(Middleware\HandleErrors::class));
$pipe->pipe(new HttpMiddleware\HandleErrors(
$app->make(Registry::class),
$app->make(JsonApiRenderer::class),
$app->tagged(Reporter::class)
));

$pipe->pipe($app->make(HttpMiddleware\ParseJsonBody::class));
$pipe->pipe($app->make(Middleware\FakeHttpMethods::class));
Expand All @@ -68,25 +73,6 @@ public function register()
$this->app->afterResolving('flarum.api.middleware', function (MiddlewarePipe $pipe) {
$pipe->pipe(new HttpMiddleware\DispatchRoute($this->app->make('flarum.api.routes')));
});

$this->app->singleton(ErrorHandler::class, function () {
$handler = new ErrorHandler;

$handler->registerHandler(new ExceptionHandler\FloodingExceptionHandler);
$handler->registerHandler(new ExceptionHandler\IlluminateValidationExceptionHandler);
$handler->registerHandler(new ExceptionHandler\InvalidAccessTokenExceptionHandler);
$handler->registerHandler(new ExceptionHandler\InvalidConfirmationTokenExceptionHandler);
$handler->registerHandler(new ExceptionHandler\MethodNotAllowedExceptionHandler);
$handler->registerHandler(new ExceptionHandler\ModelNotFoundExceptionHandler);
$handler->registerHandler(new ExceptionHandler\PermissionDeniedExceptionHandler);
$handler->registerHandler(new ExceptionHandler\RouteNotFoundExceptionHandler);
$handler->registerHandler(new ExceptionHandler\TokenMismatchExceptionHandler);
$handler->registerHandler(new ExceptionHandler\ValidationExceptionHandler);
$handler->registerHandler(new InvalidParameterExceptionHandler);
$handler->registerHandler(new ExceptionHandler\FallbackExceptionHandler($this->app->inDebugMode(), $this->app->make('log')));

return $handler;
});
}

/**
Expand Down
32 changes: 13 additions & 19 deletions src/Api/Client.php
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -69,23 +72,14 @@ public function send($controller, User $actor = null, array $queryParams = [], a

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;
}
}
51 changes: 0 additions & 51 deletions src/Api/ErrorHandler.php

This file was deleted.

7 changes: 6 additions & 1 deletion src/Api/Exception/InvalidAccessTokenException.php
Expand Up @@ -12,7 +12,12 @@
namespace Flarum\Api\Exception;

use Exception;
use Flarum\Foundation\KnownError;

class InvalidAccessTokenException extends Exception
class InvalidAccessTokenException extends Exception implements KnownError
{
public function getType(): string
{
return 'invalid_access_token';
}
}
77 changes: 0 additions & 77 deletions src/Api/ExceptionHandler/FallbackExceptionHandler.php

This file was deleted.

42 changes: 0 additions & 42 deletions src/Api/ExceptionHandler/FloodingExceptionHandler.php

This file was deleted.

58 changes: 0 additions & 58 deletions src/Api/ExceptionHandler/IlluminateValidationExceptionHandler.php

This file was deleted.