This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36dfe0d
commit 67409c9
Showing
9 changed files
with
499 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# ApiExceptionMiddleware | ||
|
||
```php | ||
<?php | ||
|
||
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface; | ||
use Chubbyphp\ApiHttp\Middleware\ApiExceptionMiddleware; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** @var ServerRequestInterface $request */ | ||
$request = ...; | ||
|
||
/** @var RequestHandlerInterface $handler */ | ||
$handler = ...; | ||
|
||
/** @var ResponseManagerInterface $responseManager */ | ||
$responseManager = ...; | ||
|
||
/** @var LoggerInterface $logger */ | ||
$logger = ...; | ||
|
||
$middleware = new ApiExceptionMiddleware( | ||
$responseManager, | ||
true, | ||
$logger | ||
); | ||
|
||
/** @var ResponseInterface $response */ | ||
$response = $middleware->process($request, $handler); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chubbyphp\ApiHttp\Middleware; | ||
|
||
use Chubbyphp\ApiHttp\ApiProblem\ServerError\InternalServerError; | ||
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
|
||
final class ApiExceptionMiddleware implements MiddlewareInterface | ||
{ | ||
/** | ||
* @var ResponseManagerInterface | ||
*/ | ||
private $responseManager; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $debug; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
public function __construct( | ||
ResponseManagerInterface $responseManager, | ||
bool $debug = false, | ||
?LoggerInterface $logger = null | ||
) { | ||
$this->responseManager = $responseManager; | ||
$this->debug = $debug; | ||
$this->logger = $logger ?? new NullLogger(); | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
try { | ||
return $handler->handle($request); | ||
} catch (\Throwable $exception) { | ||
return $this->handleException($request, $exception); | ||
} | ||
} | ||
|
||
private function handleException(ServerRequestInterface $request, \Throwable $exception): ResponseInterface | ||
{ | ||
$backtrace = $this->backtrace($exception); | ||
|
||
$this->logger->error('Exception', ['backtrace' => $backtrace]); | ||
|
||
if (null === $accept = $request->getAttribute('accept')) { | ||
throw $exception; | ||
} | ||
|
||
if ($this->debug) { | ||
$internalServerError = new InternalServerError($exception->getMessage()); | ||
$internalServerError->setBacktrace($backtrace); | ||
} else { | ||
$internalServerError = new InternalServerError(); | ||
} | ||
|
||
return $this->responseManager->createFromApiProblem($internalServerError, $accept); | ||
} | ||
|
||
/** | ||
* @return array<int, array<string, mixed>> | ||
*/ | ||
private function backtrace(\Throwable $exception): array | ||
{ | ||
$exceptions = []; | ||
do { | ||
$exceptions[] = [ | ||
'class' => get_class($exception), | ||
'message' => $exception->getMessage(), | ||
'code' => $exception->getCode(), | ||
'file' => $exception->getFile(), | ||
'line' => $exception->getLine(), | ||
'trace' => $exception->getTraceAsString(), | ||
]; | ||
} while ($exception = $exception->getPrevious()); | ||
|
||
return $exceptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.