| @@ 12-66 (lines=55) @@ | ||
| 9 | use Psr\Log\LoggerInterface; |
|
| 10 | use Psr\Log\NullLogger; |
|
| 11 | ||
| 12 | final class SimpleErrorHandler implements ErrorHandlerInterface |
|
| 13 | { |
|
| 14 | /** |
|
| 15 | * @var ErrorResponseProviderInterface |
|
| 16 | */ |
|
| 17 | private $provider; |
|
| 18 | ||
| 19 | /** |
|
| 20 | * @var LoggerInterface |
|
| 21 | */ |
|
| 22 | private $logger; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @param ErrorResponseProviderInterface $provider |
|
| 26 | */ |
|
| 27 | public function __construct(ErrorResponseProviderInterface $provider, LoggerInterface $logger = null) |
|
| 28 | { |
|
| 29 | $this->provider = $provider; |
|
| 30 | $this->logger = $logger ?? new NullLogger(); |
|
| 31 | } |
|
| 32 | ||
| 33 | /** |
|
| 34 | * @param Request $request |
|
| 35 | * @param Response $response |
|
| 36 | * @param \Exception $exception |
|
| 37 | * |
|
| 38 | * @return Response |
|
| 39 | */ |
|
| 40 | public function __invoke(Request $request, Response $response, \Exception $exception): Response |
|
| 41 | { |
|
| 42 | $this->logException($exception); |
|
| 43 | ||
| 44 | return $this->provider->get($request, $response, $exception); |
|
| 45 | } |
|
| 46 | ||
| 47 | /** |
|
| 48 | * @param \Exception $exception |
|
| 49 | */ |
|
| 50 | private function logException(\Exception $exception) |
|
| 51 | { |
|
| 52 | if ($exception instanceof HttpException) { |
|
| 53 | $this->logger->warning( |
|
| 54 | 'error-handler: {code} {message}', |
|
| 55 | ['status' => $exception->getCode(), 'message' => $exception->getMessage()] |
|
| 56 | ); |
|
| 57 | ||
| 58 | return; |
|
| 59 | } |
|
| 60 | ||
| 61 | $this->logger->error( |
|
| 62 | 'error-handler: {code} {message}', |
|
| 63 | ['status' => 500, 'message' => $exception->getMessage()] |
|
| 64 | ); |
|
| 65 | } |
|
| 66 | } |
|
| 67 | ||
| @@ 17-71 (lines=55) @@ | ||
| 14 | /** |
|
| 15 | * @deprecated use Chubbyphp\ErrorHandler\SimpleErrorHandler |
|
| 16 | */ |
|
| 17 | final class SimpleErrorHandler implements ErrorHandlerInterface |
|
| 18 | { |
|
| 19 | /** |
|
| 20 | * @var ErrorResponseProviderInterface |
|
| 21 | */ |
|
| 22 | private $provider; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @var LoggerInterface |
|
| 26 | */ |
|
| 27 | private $logger; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @param ErrorResponseProviderInterface $provider |
|
| 31 | */ |
|
| 32 | public function __construct(ErrorResponseProviderInterface $provider, LoggerInterface $logger = null) |
|
| 33 | { |
|
| 34 | $this->provider = $provider; |
|
| 35 | $this->logger = $logger ?? new NullLogger(); |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * @param Request $request |
|
| 40 | * @param Response $response |
|
| 41 | * @param \Exception $exception |
|
| 42 | * |
|
| 43 | * @return Response |
|
| 44 | */ |
|
| 45 | public function __invoke(Request $request, Response $response, \Exception $exception): Response |
|
| 46 | { |
|
| 47 | $this->logException($exception); |
|
| 48 | ||
| 49 | return $this->provider->get($request, $response, $exception); |
|
| 50 | } |
|
| 51 | ||
| 52 | /** |
|
| 53 | * @param \Exception $exception |
|
| 54 | */ |
|
| 55 | private function logException(\Exception $exception) |
|
| 56 | { |
|
| 57 | if ($exception instanceof HttpException) { |
|
| 58 | $this->logger->warning( |
|
| 59 | 'error-handler: {code} {message}', |
|
| 60 | ['status' => $exception->getCode(), 'message' => $exception->getMessage()] |
|
| 61 | ); |
|
| 62 | ||
| 63 | return; |
|
| 64 | } |
|
| 65 | ||
| 66 | $this->logger->error( |
|
| 67 | 'error-handler: {code} {message}', |
|
| 68 | ['status' => 500, 'message' => $exception->getMessage()] |
|
| 69 | ); |
|
| 70 | } |
|
| 71 | } |
|
| 72 | ||