Create the unified exception to throw.

@param Request  $request
@param Response $response

@return HttpRequestException
353346
protected function createApiException(Request $request, Response $response)
    {
        //$errors = json_decode((string) $response->getBody());

        return new HttpRequestException(
            $response->getCode(),
            $response->getBody(),
            $request,
            $response
        );
    }
206554
public static function create(
        int $statusCode,
        string $message,
        string $request,
        string $response
    ): ApiClientException {
        switch ($statusCode) {
            case 400:
                $exception = new BadRequestException($message, $request, $response);
                break;
            case 401:
                $exception = new UnauthorizedException($message, $request, $response);
                break;
            case 403:
                $exception = new ForbiddenException($message, $request, $response);
                break;
            case 404:
                $exception = new NotFoundException($message, $request, $response);
                break;
            default:
                $exception = new ApiClientException($message, $statusCode, $request, $response);
                break;
        }
        return $exception;
    }
621593
private function checkException($response)
    {
        if ((true === $this->getThrowExceptions()) and ($this->isErroneousResponse($response))) {
            throw new ClientException($response->detail, $response->status);
        }
        return $response;
    }
1035573
private function handleError(RequestInterface $request, ResponseInterface $response, \Throwable $throwable = null): void
    {
        $errorControllerClass = $this->getErrorControllerClass();

        if ($errorControllerClass !== null) {
            /** @var ErrorControllerInterface $errorController */
            $errorController = new $errorControllerClass();
            if ($throwable !== null) {
                $errorController->setThrowable($throwable);
            }

            try {
                $errorController->processRequest($this, $request, $response, strval($response->getStatusCode()->getCode()), []);
            } catch (\Throwable $throwable) {
                $this->throwableToResponse($throwable, $response);
            }
        }
    }
1189793
public function handleException(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
    {
        return $this->withError($response, $exception);
    }
581342
public function throwServiceExceptionFromRequestException(RequestException $exception)
    {
        $exceptionClass = ExceptionInterface::class;

        if ($exception->hasResponse() && $exception->getResponse()->hasHeader('X-WS-ErrorCode')) {
            $exceptionClass = $this->getServiceException($exception->getResponse()->getHeaderLine('X-WS-ErrorCode'));
        }
        if ($exceptionClass !== ExceptionInterface::class) {
            throw new $exceptionClass();
        }

        throw new ServerException('Unknown error occurred', 0, $exception);
    }
1062724
protected function unauthorizedException(Request $request, Response $response, $message = 'Unauthorized.')
    {
        return new UnauthorizedException($request, $response, $message);
    }
440077
private function throwFromErrorResponse(HttpResponse $response, array $body)
    {
        list($message, $code, $extra) = $this->getErrorStringFromResponseBody($body);

        $exceptions = [
            400 => BadRequest::class,
            401 => Unauthorized::class,
            403 => Forbidden::class,
            404 => NotFound::class,
            406 => NotAcceptable::class,
            410 => Gone::class,
            420 => RateLimitTriggered::class,
            422 => UnprocessableEntity::class,
            429 => RateLimitTriggered::class,
            500 => ServerError::class,
            502 => BadGateway::class,
            503 => ServiceUnavailable::class,
            504 => GatewayTimeout::class,
        ];

        if (!array_key_exists($response->getStatus(), $exceptions)) {
            return;
        }

        throw new $exceptions[$response->getStatus()]($message, $code, null, $extra);
    }
574112
protected function handleRequestException(Request $request, RequestException $previousException)
    {
        if (null !== $previousException) {
            $response = $previousException->getResponse();

            if (null == $response || ($response->getStatusCode() >= 500 && $response->getStatusCode() <= 599)) {
                throw new NullResponseException($this->request, $previousException);
            }
        }

        throw $this->createApiException(
            $request,
            $this->createResponse($previousException->getResponse()),
            $previousException
        );
    }
355697
protected function processActionExecute(Request $request, Response $response, Action $action) {
        $forward = $throwable = null;

        // Alles kapseln, damit Postprocessing-Hook auch nach Auftreten einer Exception aufgerufen
        // werden kann (z.B. Transaction-Rollback o.ae.)
        try {
            // allgemeinen Preprocessing-Hook aufrufen
            $forward = $action->executeBefore($request, $response);

            // Action nur ausfuehren, wenn executeBefore() nicht schon Abbruch signalisiert hat
            if ($forward === null) {
                // TODO: implement dispatching for DispatchActions; as of now it must be done manually in execute()
                $forward = $action->execute($request, $response);
            }
        }
        catch (\Exception $ex) {
            $throwable = $ex;    // evt. aufgetretene Exception zwischenspeichern
        }

        // falls statt eines ActionForwards ein String-Identifier zurueckgegeben wurde, diesen aufloesen
        if (is_string($forward)) {
            if ($forwardInstance = $action->getMapping()->findForward($forward)) {
                $forward = $forwardInstance;
            }
            else {
                $throwable = new RuntimeException('No ActionForward found for name "'.$forward.'"');
                $forward = null;
            }
        }

        // allgemeinen Postprocessing-Hook aufrufen
        $forward = $action->executeAfter($request, $response, $forward);

        // jetzt evt. aufgetretene Exception weiterreichen
        if ($throwable)
            throw $throwable;

        return $forward;
    }
439821
private function createStatusCodeException(
        ResponseInterface $response,
        InternalRequestInterface $internalRequest,
        HttpAdapterInterface $httpAdapter
    ) {
        $exception = HttpAdapterException::cannotFetchUri(
            (string) $internalRequest->getUri(),
            $httpAdapter->getName(),
            sprintf('Status code: %d', $response->getStatusCode())
        );

        $exception->setRequest($internalRequest);
        $exception->setResponse($response);

        return $exception;
    }
202565
private static function createException(RequestInterface $request,
        ResponseInterface $altered,
        \Exception $previous = null,
        array $ctx = []
    ) {
        if ($altered->getStatusCode() >= 400 && $altered->getStatusCode() < 500) {
            return new ClientException($request, $altered, $previous, $ctx);
        }

        return new ServerException($request, $altered, $previous, $ctx);
    }
227002
public function resolve(Request $request, Response $response)
    {
        if ($this->authorized) {
            return $this->actualResource->resolve($request, $response);
        }

        return $this->error;
    }
226442
protected function duplicateRequest(\Exception $exception, Request $request)
    {
        $attributes = [
            'exception' => $exception = FlattenException::create($exception),
            '_controller' => $this->controller ?: function () use ($exception) {
                $handler = new ExceptionHandler($this->debug, $this->charset, $this->fileLinkFormat);

                return new Response($handler->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders());
            },
            'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
        ];
        $request = $request->duplicate(null, null, $attributes);
        $request->setMethod('GET');

        return $request;
    }
890144
public static function create(Response $response)
    {
        $errorMessage = $response->getError();
        $exception = isset(static::$exceptionMap[$errorMessage])
            ? static::$exceptionMap[$errorMessage]
            : CentrifugoException::class;

        return new static($response, new $exception($errorMessage));
    }
1240763
private function throwRequestException($statusCode, $response)
    {
        if ($response && isset($response->error)) {
            $type = $response->error->type;
            $message = $this->getResponseErrorMessage($response->error);
            $link = $response->error->link;
        } elseif ($this->isServerErrorResponse($statusCode)) {
            $type = 'Server Error';
            $message = RequestException::$statusTexts[$statusCode];
            $link = '';
        } else {
            $type = 'Client Error';
            $message = RequestException::$statusTexts[$statusCode];
            $link = '';
        }

        throw new RequestException($type, $message, $link, $statusCode);
    }
221497
public function collect(Request $request, Response $response, \Exception $exception = null)
    {
        $mapper = new ArrayMapper();
        $this->data = $mapper->toArray($this->byteCodeCache);
    }