mirror of
https://github.com/AzuraCast/AzuraCast.git
synced 2024-06-14 13:16:37 +00:00
Better error handling through wrapping exceptions with richer requests.
This commit is contained in:
parent
a283f7d5ef
commit
0889916194
|
@ -65,6 +65,8 @@ return function (App\EventDispatcher $dispatcher) {
|
|||
call_user_func(include(__DIR__ . '/routes.dev.php'), $app);
|
||||
}
|
||||
|
||||
$app->add(Middleware\WrapExceptionsWithRequestData::class);
|
||||
|
||||
$app->add(Middleware\EnforceSecurity::class);
|
||||
$app->add(Middleware\InjectAcl::class);
|
||||
$app->add(Middleware\GetCurrentUser::class);
|
||||
|
@ -84,12 +86,12 @@ return function (App\EventDispatcher $dispatcher) {
|
|||
$app->add(new Middleware\RemoveSlashes);
|
||||
$app->add(new Middleware\ApplyXForwardedProto);
|
||||
|
||||
// Error handling, which should always be near the "last" element.
|
||||
$errorMiddleware = $app->addErrorMiddleware(!$settings->isProduction(), true, true);
|
||||
$errorMiddleware->setDefaultErrorHandler(Slim\Interfaces\ErrorHandlerInterface::class);
|
||||
|
||||
// Use PSR-7 compatible sessions.
|
||||
$app->add(Middleware\InjectSession::class);
|
||||
|
||||
// Add an error handler for most in-controller/task situations.
|
||||
$errorMiddleware = $app->addErrorMiddleware(!$settings->isProduction(), true, true);
|
||||
$errorMiddleware->setDefaultErrorHandler(Slim\Interfaces\ErrorHandlerInterface::class);
|
||||
});
|
||||
|
||||
// Build default menus
|
||||
|
|
16
src/Exception/WrappedException.php
Normal file
16
src/Exception/WrappedException.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
namespace App\Exception;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Slim\Exception\HttpException;
|
||||
use Throwable;
|
||||
|
||||
class WrappedException extends HttpException
|
||||
{
|
||||
public function __construct(
|
||||
ServerRequestInterface $request,
|
||||
?Throwable $previous
|
||||
) {
|
||||
parent::__construct($request, 'Wrapped HTTP Exception', 500, $previous);
|
||||
}
|
||||
}
|
|
@ -55,6 +55,10 @@ class ErrorHandler extends \Slim\Handlers\ErrorHandler
|
|||
bool $logErrors,
|
||||
bool $logErrorDetails
|
||||
): ResponseInterface {
|
||||
if ($exception instanceof Exception\WrappedException) {
|
||||
$exception = $exception->getPrevious();
|
||||
}
|
||||
|
||||
if ($exception instanceof Exception) {
|
||||
$this->loggerLevel = $exception->getLoggerLevel();
|
||||
} elseif ($exception instanceof HttpException) {
|
||||
|
@ -143,15 +147,19 @@ class ErrorHandler extends \Slim\Handlers\ErrorHandler
|
|||
));
|
||||
}
|
||||
|
||||
$view = $this->viewFactory->create($this->request);
|
||||
try {
|
||||
$view = $this->viewFactory->create($this->request);
|
||||
|
||||
return $view->renderToResponse(
|
||||
$response,
|
||||
'system/error_http',
|
||||
[
|
||||
'exception' => $this->exception,
|
||||
]
|
||||
);
|
||||
return $view->renderToResponse(
|
||||
$response,
|
||||
'system/error_http',
|
||||
[
|
||||
'exception' => $this->exception,
|
||||
]
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
return parent::respond();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->exception instanceof NotLoggedInException) {
|
||||
|
@ -240,15 +248,19 @@ class ErrorHandler extends \Slim\Handlers\ErrorHandler
|
|||
return $response->write($run->handleException($this->exception));
|
||||
}
|
||||
|
||||
$view = $this->viewFactory->create($this->request);
|
||||
try {
|
||||
$view = $this->viewFactory->create($this->request);
|
||||
|
||||
return $view->renderToResponse(
|
||||
$response,
|
||||
'system/error_general',
|
||||
[
|
||||
'exception' => $this->exception,
|
||||
]
|
||||
);
|
||||
return $view->renderToResponse(
|
||||
$response,
|
||||
'system/error_general',
|
||||
[
|
||||
'exception' => $this->exception,
|
||||
]
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
return parent::respond();
|
||||
}
|
||||
}
|
||||
|
||||
protected function withJson(ResponseInterface $response, $data): ResponseInterface
|
||||
|
|
29
src/Middleware/WrapExceptionsWithRequestData.php
Normal file
29
src/Middleware/WrapExceptionsWithRequestData.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
namespace App\Middleware;
|
||||
|
||||
use App\Exception\WrappedException;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
/**
|
||||
* Wrap all exceptions thrown past this point with rich metadata.
|
||||
*/
|
||||
class WrapExceptionsWithRequestData implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @param RequestHandlerInterface $handler
|
||||
*
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
try {
|
||||
return $handler->handle($request);
|
||||
} catch (\Throwable $e) {
|
||||
throw new WrappedException($request, $e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user