Creates local exception from guzzle request exception, which includes
response body.

@param  RequestException  $requestException

@return void
@throws Exceptions\Exception
489994
protected function throwRequestException(RequestException $requestException)
    {
        $exceptionParts = $this->getRequestExceptionParts($requestException);

        $exception = new Exceptions\Exception(
            $exceptionParts['reason'],
            $exceptionParts['code'],
            $requestException
        );

        $body = $exceptionParts['body'];
        $json = json_decode($body);

        if (json_last_error() == JSON_ERROR_NONE) {
            throw $exception->setResponseBody($json);
        }

        throw $exception->setResponseBody($body);
    }
791033
public function handleException($exception, $request, $response){
        // send a debug mail with the exception
        if(!$this->debug && !in_array(get_class($exception), $this->_skipExceptions))
            $this->_email('Website Exception', Misc::dump($exception, $exception->getMessage(), true));

        // continue with exception handle logic
        return parent::handleException($exception, $request, $response);
    }
710915
protected function canRetry(RequestException $exception)
    {
        if ($exception instanceof NetworkException) {
            if ($exception->getCode() === 0 || $exception->getCode() >= 500) {
                return true;
            }
        } elseif ($exception instanceof ApiException) {
            return in_array($exception->getCode(), [
                ApiException::AUTHENTICATION_TEMPORARILY_UNAVAILABLE,
                ApiException::SERVER_TEMPORARILY_UNAVAILABLE,
                ApiException::INITIALIZATION_ERROR,
                ApiException::OPERATION_ERROR,
                ApiException::EXCEEDED_LIMIT_CONCURRENT_REQUESTS,
                ApiException::TEMPORARILY_UNAVAILABLE,
            ]);
        }

        return false;
    }
1243295
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        $errorMessage = 'Authentication Failed';
        $responseCode = Response::HTTP_BAD_REQUEST;

        if ($exception instanceof TokenExpiredException) {
            $errorMessage = 'Token expired';
            $responseCode = Response::HTTP_UNAUTHORIZED;
        } elseif ($exception instanceof AccountStatusException) {
            $errorMessage = 'Account disabled';
            $responseCode = Response::HTTP_FORBIDDEN;
        }

        return new JsonResponse(['error' => $errorMessage], $responseCode);
    }
1130371
private function handleException(GuzzleExceptions\TransferException $exception, RequestInterface $request)
    {
        if ($exception instanceof GuzzleExceptions\ConnectException) {
            return new HttplugException\NetworkException($exception->getMessage(), $request, $exception);
        }

        if ($exception instanceof GuzzleExceptions\RequestException) {
            // Make sure we have a response for the HttpException
            if ($exception->hasResponse()) {
                $psr7Response = $this->createResponse($exception->getResponse());

                return new HttplugException\HttpException(
                    $exception->getMessage(),
                    $request,
                    $psr7Response,
                    $exception
                );
            }

            return new HttplugException\RequestException($exception->getMessage(), $request, $exception);
        }

        return new HttplugException\TransferException($exception->getMessage(), 0, $exception);
    }
1266898
private function createHttpExceptionResponse(HttpException $exception)
    {
        return new JsonResponse(
            [
                'status'  => 'ERROR',
                'message' => $exception->getMessage()
            ],
            $exception->getStatusCode(),
            $exception->getHeaders()
        );
    }
316291
public function addException(RequestInterface $request, ClientExceptionInterface $exception): self
    {
        $new = clone $this;
        $new->exceptions->attach($request, $exception);

        return $new;
    }
1153378
public function applyResponse(HttpRequestInterface $request, HttpResponseInterface $response, array $options)
    {
        if ($response instanceof ResponseInterface &&
            isset($options['rpc_error']) &&
            true === $options['rpc_error'] &&
            null !== $response->getRpcErrorCode()
        ) {
            throw RequestException::create($request, $response);
        }

        return $response;
    }
914809
public function crawlFailed(
        UriInterface $url,
        RequestException $requestException,
        ?UriInterface $foundOnUrl = null
    ) {
        $url->foundOnUrl = $foundOnUrl;

        return parent::crawlFailed($url, $requestException, $foundOnUrl);
    }
864800
private function _throwServerError(\Exception $e) {
		$uri = (string) $this->httpClient->getConfig('base_uri');

		throw new ServerErrorHttpException(get_class($e).': url='.$uri .' '. $e->getMessage(), 500);
	}
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
        );
    }
671170
public static function sendException($exception)
    {
        $emails = Config::inst()->get(self::class, 'Emails');
        if ($emails) {
            $emails = explode(",", $emails);
            $email = new Email();
            $email
                ->setTo($emails)
                ->setSubject('SilverStripe Hail module fetch error on ' . SiteConfig::current_site_config()->getTitle() . ' (' . gethostname() . ')')
                ->setBody("<p>Hi,</p><p>An error occurred while fetching from the Hail API: </p> <p>{$exception->getMessage()}</p><p>Website name: " . SiteConfig::current_site_config()->getTitle() . "</p><p>Website Folder: " . Director::baseFolder() . "</p><p>Server hostname: " . gethostname() . "</p>");
            $email->send();
        }
    }
353346
protected function createApiException(Request $request, Response $response)
    {
        //$errors = json_decode((string) $response->getBody());

        return new HttpRequestException(
            $response->getCode(),
            $response->getBody(),
            $request,
            $response
        );
    }
897664
public function onException(GetResponseForExceptionEvent $event): void
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        $attributes = $event->getRequest()->attributes;
        if ($attributes->get(SetIsApiRequestListener::API_FLAG_NAME) !== true) {
            return;
        }

        $exception = $event->getException();
        if ($exception instanceof HttpExceptionInterface) {
            return;
        }

        $exceptionClass = null;
        foreach ($this->exceptionMap as $sourceException => $targetException) {
            if (is_a($exception, $sourceException, true)) {
                $exceptionClass = $targetException;

                break;
            }
        }

        if ($exceptionClass !== null) {
            $convertedException = new $exceptionClass(
                $exception->getMessage(),
                $exception,
                $exception->getCode()
            );

            $event->setException($convertedException);
        }
    }
894980
public static function createFromException(Exception $exception, Request $request) {
        $date = new DateTime('now', new DateTimeZone('UTC'));

        $model = new self();
        $model->setHttpCode($exception->getCode())
              ->setErrorMessage($exception->getMessage())
              ->setDate($date)
              ->setImboErrorCode($exception->getImboErrorCode() ?: Exception::ERR_UNSPECIFIED);

        if ($image = $request->getImage()) {
            $model->setImageIdentifier($image->getImageIdentifier());
        } else if ($identifier = $request->getImageIdentifier()) {
            $model->setImageIdentifier($identifier);
        }

        return $model;
    }
11784
public static function parseError(RequestException $requestException, $isAssoc = true)
	{

		$error = $error = json_decode($requestException->getResponse()->getBody(), $isAssoc);

		if (!is_null($error))
		{
			return $error;
		}
		else
		{
			return $requestException->getMessage();
		}
	}
63597
public static function wrap(RequestException $e)
    {
        $response = $e->getResponse();

        if ($errors = self::errors($response)) {
            $class = self::exceptionClass($response, $errors[0]);
            $message = implode(', ', array_map('strval', $errors));
        } else {
            $class = self::exceptionClass($response);
            $message = $e->getMessage();
        }

        return new $class($message, $errors, $e->getRequest(), $response, $e);
    }