setResponseFactory($factory) ->setLogger($logger); } protected ResponseFactoryInterface $factory; protected LoggerInterface $logger; public function getResponseFactory(): ResponseFactoryInterface { return $this->factory; } public function getLogger(): LoggerInterface { return $this->logger; } public function setResponseFactory(ResponseFactoryInterface $factory): CustomExceptions { $this->factory = $factory; return $this; } public function setLogger(LoggerInterface $logger): CustomExceptions { $this->logger = $logger; return $this; } public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { try { return $handler->handle($request); } catch (BlankResult $e) { $this->getLogger()->error($e); return $this->getResponseFactory()->createResponse(204); } catch (JsonException $e) { $this->getLogger()->error($e); return $this->getResponseFactory()->createResponse(415, 'Only JSON Media Type is supported for this request'); } catch (MissingArgument $e) { $this->getLogger()->error($e); return $this->getResponseFactory()->createResponse(400, $e->getMessage()); } } }