Get the route to the error page

@param ServerRequestInterface $request
@param ResponseInterface $response
@return Router\Route
470
protected function getErrorRoute(ServerRequestInterface $request, ResponseInterface $response)
    {
        if (
            interface_exists('Jasny\HttpMessage\GlobalEnvironmentInterface') &&
            $request instanceof GlobalEnvironmentInterface
        ) {
            $request = $request->withoutGlobalEnvironment();
        }
        
        $status = $response->getStatusCode();
        
        $errorUri = $request->getUri()
            ->withPath($status)
            ->withQuery(null)
            ->withFragment(null);
        
        return $this->router->getRoutes()->getRoute($request->withUri($errorUri));
    }
445410
private function processError(ServerRequestInterface $request, ResponseInterface $response, array $arguments = null)
    {
        if (is_callable($this->options["error"])) {
            $handler_response = $this->options["error"]($request, $response, $arguments);
            if (is_a($handler_response, "\Psr\Http\Message\ResponseInterface")) {
                return $handler_response;
            }
        }
        return $response;
    }
1200992
public function dispatch(HttpRequest $request, HttpResponse $response)
    {
        if (false === $this->booted) {
            $this->boot();
        }


        // cache like a mother fucker
//        if(!$this->hasRouteInCache($request)) {
            $routeParams = $this->handleRouting($request);
//            $this->setRouteInCache($request, $routeParams);
            // @todo - move these 2 lines to setRouteInCache()
//            $routingCache = $this->serviceManager->get('RoutingCache');
//            $routingCache->set($request->getPathInfo(), $routeParams);
//        } else {
//            $routeParams = $this->getRouteFromCache($request);
//        }

        $request->attributes->add($routeParams);

        // Resolve our Controller
        $resolver = $this->serviceManager->get('ControllerResolver');
        if (false === $controller = $resolver->getController($request)) {
            throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s".', $request->getPathInfo()));
        }

        $controllerArguments = $resolver->getArguments($request, $controller);

        $result = call_user_func_array(
            $controller,
            $controllerArguments
        );

        if ($result === null) {
            throw new \Exception('Your action returned null. It must always return something');
        } elseif (is_string($result)) {
            $response->setContent($result);
        } elseif ($result instanceof SymfonyResponse || $response instanceof HttpResponse) {
            $response = $result;
        } else {
            throw new \Exception('Invalid response type returned from controller');
        }

        return $response;
    }
1328009
public function match(ServerRequestInterface $request) : RouterMatch
    {
        $path = $request->getUri()->getPath();
        $syContext = new RequestContext('/');
        $syMatcher = new UrlMatcher($this->syRouteCollection, $syContext);
        try {
            $params = $syMatcher->match($path);
        } catch (\Throwable $e) {
            throw new HttpException(404, "No route matched for path $path",
                null, $e);
        }
        
        $matchedRouteName = $params['_route'];
        unset($params['_route']);
        $route = $this->routeProvider->getRouteFromName($matchedRouteName);
        $routerMatch = new RouterMatch($route, $params);
        return $routerMatch;
    }
174730
protected function notFound(ServerRequestInterface $request, ResponseInterface $response)
    {
        $finalResponse = $response
            ->withProtocolVersion($request->getProtocolVersion())
            ->withStatus(404)
            ->withHeader('Content-Type', 'text/plain');
        
        $finalResponse->getBody()->write("Not found");
        
        return $finalResponse;
    }
1014203
public function dispatchRequest(ServerRequestInterface $request) : ResponseInterface
    {
        $match = $this->dispatch($request->getMethod(), $request->getUri()->getPath());

        switch ($match[0]) {
            case FastRoute::NOT_FOUND:
                $this->setNotFoundDecoratorMiddleware();
                break;
            case FastRoute::METHOD_NOT_ALLOWED:
                $allowed = (array) $match[1];
                $this->setMethodNotAllowedDecoratorMiddleware($allowed);
                break;
            case FastRoute::FOUND:
                $match[1]->setVars($match[2]);
                $this->setFoundMiddleware($match[1]);
                break;
        }

        return $this->handle($request);
    }
494680
public function options( ServerRequestInterface $request, ResponseInterface $response )
	{
		return $this->client->options( $request, $response );
	}
583280
protected function onErrorResponse(CommandInterface $command, ErrorResponseInterface $response)
    {
        if ($command instanceof ScriptCommand && $response->getErrorType() === 'NOSCRIPT') {
            $eval = $this->createCommand('EVAL');
            $eval->setRawArguments($command->getEvalArguments());

            $response = $this->executeCommand($eval);

            if (!$response instanceof ResponseInterface) {
                $response = $command->parseResponse($response);
            }

            return $response;
        }

        if ($this->options->exceptions) {
            throw new ServerException($response->getMessage());
        }

        return $response;
    }
53043
public function handle(ServerRequestInterface $request): ResponseInterface
    {
        try {
            $route = $this->matchRoute($request);
        } catch (RouteException $e) {
            throw new RouterException("Invalid route definition", $e->getCode(), $e);
        }

        if (empty($route)) {
            throw new RouteNotFoundException($request->getUri());
        }

        return $this->container->get(ScopeInterface::class)->runScope(
            [RouteInterface::class => $route],
            function () use ($route, $request) {
                return $route->handle($request);
            }
        );
    }
78063
public function handle(
        ServerRequestInterface $request, ResponseInterface $response
    )
    {
        /** @var Route $route */
        $route = $request->getAttribute('route', false);
        $this->setAttributes($route);
        $class = $this->namespace.'\\'.$this->controller;
        try {
            $controller = $this->createController($class);
            $controller->register($request, $response);

            $this->checkAction($class);

            call_user_func_array([$controller, $this->action], $this->args);
            $request = $controller->getRequest();
            $request = $this->setViewVars($controller, $request);
            $response = $controller->getResponse();
        } catch (ControllerNotFoundException $caught) {
            $this->checkView($request, $caught);
        }

        return $this->executeNext($request, $response);
    }
374359
public function match(ServerRequestInterface $request)
    {
        $basePathLength = strlen($this->baseUri['path']);

        return RouteCollectionMatcher::matchRouteCollection(
            $this->routeCollection,
            $request,
            $basePathLength,
            []
        ) ?: MatchResult::fromMatchFailure();
    }
1130058
protected function handleErrors(ResponseInterface $response)
    {
        $body = $response->getBody()->__toString();
        switch ($response->getStatusCode()) {
            case 400:
                throw new DomainExceptions\BadRequestException($body);
                break;

            case 403:
                throw new DomainExceptions\ForbiddenException();
                break;

            case 404:
                throw new DomainExceptions\NotFoundException();
                break;

            case 500:
                throw new DomainExceptions\ServerErrorException();
                break;

            default:
                throw new DomainExceptions\UnknownErrorException();
                break;
        }
    }
882595
private function exec(
        RouteInterface $route,
        RequestInterface $request,
        ResponseInterface $response,
        bool $error = false
    ) {
        $variables = $route->getVariables();
        $callback = $route->getCallback();

        // If extracted call back is string
        if (\is_string($callback)) {

            $className = $this->extractClass($callback);

            // Then class provided
            $class = new $className();
            $action = $this->detectAction($callback, $variables);

            // If method is not found in class and error is not triggered, then trigger error
            if (true !== $error && false === $this->methodCheck($class, $action)) {
                $router = $this->container('router');
                $routeError = $router->getError();
                return $this->exec($routeError, $request, $response, true);
            }

            // Call required action, with request/response
            $class->$action($request, $response, $variables);
        } else {
            // Else simple callback
            $callback($request, $response, $variables);
        }
        return $response->getBody();
    }
492590
protected function sendVerificationFailedResponse(Request $request, $response)
    {
        return redirect($this->redirectPath())->with('error', trans($response));
    }
273696
public function get( ServerRequestInterface $request, ResponseInterface $response )
	{
		$view = $this->getView();

		$allow = false;
		$id = $view->param( 'id', 'default' );

		try
		{
			try
			{
				$view->item = $this->controller->load( $id, $this->getParts( $view ) );
			}
			catch( \Aimeos\MShop\Exception $e )
			{
				$view->item = $this->controller->setType( $id )->get();
				$allow = true;
			}

			$status = 200;
		}
		catch( \Aimeos\MShop\Exception $e )
		{
			$status = 404;
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
		}
		catch( \Exception $e )
		{
			$status = 500;
			$view->errors = $this->getErrorDetails( $e );
		}

		return $this->render( $response, $view, $status, $allow );
	}
506534
private function defaultHandler(ServerRequestInterface $request, ResponseInterface $response)
    {
        $statusCode = $response->getStatusCode();
        $exception = self::getException($request);
        $message = $exception ? $exception->getMessage() : '';

        switch (Utils\Helpers::getMimeType($response)) {
            case 'text/plain':
            case 'text/css':
            case 'text/javascript':
                return self::errorText($statusCode, $message);

            case 'image/jpeg':
                return self::errorImage($statusCode, $message, 'imagejpeg');

            case 'image/gif':
                return self::errorImage($statusCode, $message, 'imagegif');

            case 'image/png':
                return self::errorImage($statusCode, $message, 'imagepng');

            case 'image/svg+xml':
                return self::errorSvg($statusCode, $message);

            case 'application/json':
                return self::errorJson($statusCode, $message);

            case 'text/xml':
                return self::errorXml($statusCode, $message);

            default:
                return self::errorHtml($statusCode, $message);
        }
    }
597882
private function checkError(ResponseInterface $response, $url)
    {
        if (($response->getStatusCode() == 200) || ($response->getStatusCode() == 201)) {
            return;
        }

        switch ($response->getHeader('Content-Type')) {
            case 'text/plain':
                throw new \RuntimeException('Error: ' . $response->getBody() . ' URI: ' . $url);
            case 'application/json':
                $error = json_decode($response->getBody());
                if (isset($error->message)) {
                    throw new \RuntimeException($error->message . ' URI: ' . $url);
                }
                break;
            default:
                throw new \RuntimeException(
                    'Unknown Error: No error message was returned from the server - Code: ' . $response->getStatusCode(
                    ) . ' URI: ' . $url
                );
        }
    }