2022-12-20
This commit is contained in:
44
api/common/Middleware/Error.php
Normal file
44
api/common/Middleware/Error.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace Common\Middleware;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Throwable;
|
||||
use Slim\App;
|
||||
use Slim\Interfaces\ErrorHandlerInterface;
|
||||
use function Safe\error_log;
|
||||
|
||||
class Error implements ErrorHandlerInterface
|
||||
{
|
||||
public function __construct(App $app)
|
||||
{
|
||||
$this->setApp($app);
|
||||
}
|
||||
|
||||
protected App $app;
|
||||
public function setApp(App $app): Error
|
||||
{
|
||||
$this->app = $app;
|
||||
return $this;
|
||||
}
|
||||
public function getApp(): App
|
||||
{
|
||||
return $this->app;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails, bool $logErrors, bool $logErrorDetails): ResponseInterface
|
||||
{
|
||||
error_log($exception);
|
||||
$response = $this->getApp()->getResponseFactory()->createResponse($exception->getCode());
|
||||
$output = json_encode([
|
||||
'uri' => $request->getUri()->getPath(),
|
||||
'code' => $exception->getCode(),
|
||||
'message' => $exception->getMessage(),
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine(),
|
||||
'trace' => $exception->getTrace()
|
||||
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
$response->getBody()->write($output);
|
||||
return $response->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user