mirror of
https://github.com/flarum/core.git
synced 2025-07-19 15:51:16 +02:00
Log errors that occur in the API stack
This takes place only in the FallbackExceptionHandler. Having a custom exception handler implies that a friendly message is displayed in the API response, in which case we can bet that the exception won't need to be "debugged" per se.
This commit is contained in:
@@ -25,7 +25,6 @@ use Flarum\Http\RouteCollection;
|
|||||||
use Flarum\Http\RouteHandlerFactory;
|
use Flarum\Http\RouteHandlerFactory;
|
||||||
use Flarum\Http\UrlGenerator;
|
use Flarum\Http\UrlGenerator;
|
||||||
use Tobscure\JsonApi\ErrorHandler;
|
use Tobscure\JsonApi\ErrorHandler;
|
||||||
use Tobscure\JsonApi\Exception\Handler\FallbackExceptionHandler;
|
|
||||||
use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler;
|
use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler;
|
||||||
use Zend\Stratigility\MiddlewarePipe;
|
use Zend\Stratigility\MiddlewarePipe;
|
||||||
|
|
||||||
@@ -80,7 +79,7 @@ class ApiServiceProvider extends AbstractServiceProvider
|
|||||||
$handler->registerHandler(new ExceptionHandler\TokenMismatchExceptionHandler);
|
$handler->registerHandler(new ExceptionHandler\TokenMismatchExceptionHandler);
|
||||||
$handler->registerHandler(new ExceptionHandler\ValidationExceptionHandler);
|
$handler->registerHandler(new ExceptionHandler\ValidationExceptionHandler);
|
||||||
$handler->registerHandler(new InvalidParameterExceptionHandler);
|
$handler->registerHandler(new InvalidParameterExceptionHandler);
|
||||||
$handler->registerHandler(new FallbackExceptionHandler($this->app->inDebugMode()));
|
$handler->registerHandler(new ExceptionHandler\FallbackExceptionHandler($this->app->inDebugMode(), $this->app->make('log')));
|
||||||
|
|
||||||
return $handler;
|
return $handler;
|
||||||
});
|
});
|
||||||
|
79
src/Api/ExceptionHandler/FallbackExceptionHandler.php
Normal file
79
src/Api/ExceptionHandler/FallbackExceptionHandler.php
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\Api\ExceptionHandler;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
|
||||||
|
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
|
||||||
|
|
||||||
|
class FallbackExceptionHandler implements ExceptionHandlerInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $debug;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var LoggerInterface
|
||||||
|
*/
|
||||||
|
protected $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $debug
|
||||||
|
* @param LoggerInterface $logger
|
||||||
|
*/
|
||||||
|
public function __construct($debug, LoggerInterface $logger)
|
||||||
|
{
|
||||||
|
$this->debug = $debug;
|
||||||
|
$this->logger = $logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function manages(Exception $e)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function handle(Exception $e)
|
||||||
|
{
|
||||||
|
$status = 500;
|
||||||
|
$error = $this->constructError($e, $status);
|
||||||
|
|
||||||
|
if (! $this->debug) {
|
||||||
|
$this->logger->error($e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ResponseBag($status, [$error]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Exception $e
|
||||||
|
* @param $status
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function constructError(Exception $e, $status)
|
||||||
|
{
|
||||||
|
$error = ['code' => $status, 'title' => 'Internal server error'];
|
||||||
|
|
||||||
|
if ($this->debug) {
|
||||||
|
$error['detail'] = (string) $e;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $error;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user