-
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
Dominik Zogg
committed
Mar 1, 2020
1 parent
36dfe0d
commit 5bf135a
Showing
10 changed files
with
500 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
C:37:"PHPUnit\Runner\DefaultTestResultCache":1144:{a:2:{s:7:"defects";a:3:{s:104:"Chubbyphp\Tests\ApiHttp\Unit\Middleware\ExceptionMiddlewareTest::testWithoutExceptionWithDebugWithLogger";i:3;s:101:"Chubbyphp\Tests\ApiHttp\Unit\Middleware\ExceptionMiddlewareTest::testWithExceptionWithDebugWithLogger";i:3;s:114:"Chubbyphp\Tests\ApiHttp\Unit\Middleware\ExceptionMiddlewareTest::testWithExceptionWithDebugWithLoggerWithoutAccept";i:4;}s:5:"times";a:6:{s:104:"Chubbyphp\Tests\ApiHttp\Unit\Middleware\ExceptionMiddlewareTest::testWithoutExceptionWithDebugWithLogger";d:0.001;s:110:"Chubbyphp\Tests\ApiHttp\Unit\Middleware\ExceptionMiddlewareTest::testWithoutExceptionWithoutDebugWithoutLogger";d:0.059;s:101:"Chubbyphp\Tests\ApiHttp\Unit\Middleware\ExceptionMiddlewareTest::testWithExceptionWithDebugWithLogger";d:0.004;s:104:"Chubbyphp\Tests\ApiHttp\Unit\Middleware\ExceptionMiddlewareTest::testWithExceptionWithoutDebugWithLogger";d:0.002;s:107:"Chubbyphp\Tests\ApiHttp\Unit\Middleware\ExceptionMiddlewareTest::testWithExceptionWithoutDebugWithoutLogger";d:0.003;s:114:"Chubbyphp\Tests\ApiHttp\Unit\Middleware\ExceptionMiddlewareTest::testWithExceptionWithDebugWithLoggerWithoutAccept";d:0.009;}}} |
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 @@ | ||
# ExceptionMiddleware | ||
|
||
```php | ||
<?php | ||
|
||
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface; | ||
use Chubbyphp\ApiHttp\Middleware\ExceptionMiddleware; | ||
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 ExceptionMiddleware( | ||
$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 ExceptionMiddleware 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.