public function handle(
        ServerRequestInterface $request,
        ResponseInterface $response = null,
        $catch = self::DEFAULT_ERROR_CATCH
    )
    {
        /** @var ResponseInterface $response */
        // Passes the request to the container
        if (!$this->getContainer()->has(ServerRequestInterface::class)) {
            $this->getContainer()->share(ServerRequestInterface::class, $request);
        }

        //inject request content type
        $contentType = ServerRequestFactory::getHeader('content-type', $this->getRequest()->getHeaders(), '');
        $this->setContentType($contentType);

        if ($response === null) {
            $response = $this->getResponse();
        }

        $applicationEvent = $this->getApplicationEvent();
        $applicationEvent->setRequest($request);
        $applicationEvent->setResponse($response);

        try {
            $response = $this->handleResponse($this->handleRequest($request), $response);
        }catch (\Exception $exception){
            $notFoundException = $exception instanceof NotFoundException;
            $response = $this->handleError($exception, $request, $response, $catch)
                ->withStatus($notFoundException ? 404 : 500);
        }

        // validate response
        $response = $this->validateContract($response, ResponseInterface::class);

        // update content type
        $this->setContentType(ServerRequestFactory::getHeader('content-type', $response->getHeaders(), ''));

        return $response;
    }