/*
Method to extract all exceptions for Guzzle ClientException

@param ClientException $exception

@return stdClass

@throws InstagramServerException
14774
protected static function extractOriginalExceptionMessage(ClientException $exception)
    {
        self::$response = $exception->getResponse();
        self::$stream = self::$response->getBody();
        self::$content = self::$stream->getContents();
        if (!self::$content) {
            throw new InstagramServerException(
                $exception->getMessage(),
                $exception->getCode(),
                $exception
            );
        }

        return json_decode(self::$content);
    }
1291571
protected function determineException(HttpClientException $exception)
    {
        if (!empty($exception->getResponse()) && in_array($exception->getResponse()->getStatusCode(), [400, 409])) {
            return new BadRequest($exception->getResponse());
        }

        return $exception;
    }
1224053
public static function telegramRespondedWithAnError(ClientException $exception)
    {
        $statusCode = $exception->getResponse()->getStatusCode();

        $description = 'no description given';

        if ($result = json_decode($exception->getResponse()->getBody())) {
            $description = $result->description ?: $description;
        }

        return new static("Telegram responded with an error `{$statusCode} - {$description}`");
    }
710862
public function reject(ClientExceptionInterface $exception): void
    {
        if (Promise::PENDING !== $this->state) {
            return;
        }

        $this->failure = $exception;
        $this->state = Promise::REJECTED;

        foreach ($this->onRejectedCallbacks as $onRejectedCallback) {
            $onRejectedCallback($exception);
        }
    }
156310
protected function transformException(Exception $exception)
    {
        if (Request::capture()->wantsJson()) {
            $this->transformAuthException($exception);
            $this->transformEloquentException($exception);
            $this->transformValidationException($exception);
        }
    }
604586
protected static function makeExceptionContent(\Exception $exception)
    {
        if ($exception instanceof DfException) {
            return ['error' => $exception->toArray()];
        }

        $errorInfo['code'] = ($exception->getCode()) ?: ServiceResponseInterface::HTTP_INTERNAL_SERVER_ERROR;
        $errorInfo['message'] = htmlentities($exception->getMessage());

        if (config('app.debug', false)) {
            $trace = $exception->getTraceAsString();
            $trace = str_replace(["\n", "#"], ["", "<br>"], $trace);
            $traceArray = explode("<br>", $trace);
            $cleanTrace = [];
            foreach ($traceArray as $k => $v) {
                if (!empty($v)) {
                    $cleanTrace[] = $v;
                }
            }
            $errorInfo['trace'] = $cleanTrace;
        }

        return ['error' => $errorInfo];
    }
546032
private static function hasPDOException(\Exception $exception)
    {
        if ($exception instanceof \PDOException) {
            return true;
        }
        if ($exception->getPrevious() === null) {
            return false;
        }
        return self::hasPDOException($exception->getPrevious());
    }
512023
private function getExceptionStatusCode(\Exception $exception): int
    {
        if ($exception instanceof HttpExceptionInterface) {
            return $exception->getStatusCode();
        }

        if ($exception instanceof AccessDeniedException) {
            return $exception->getCode();
        }

        return Response::HTTP_INTERNAL_SERVER_ERROR;
    }
190849
public function handleException(\Exception $exception) {
    // get error level
    $logFunction = 'error';
    if ($exception instanceof ApplicationException) {
      $logFunction = $exception->getError()->getLevel() == ApplicationError::LEVEL_WARNING ?
              'warn' : 'error';
    }
    self::$logger->$logFunction($exception);

    try {
      if (ObjectFactory::getInstance('configuration') != null) {
        // rollback current transaction
        $persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
        $persistenceFacade->getTransaction()->rollback();

        // redirect to failure action
        if ($this->request) {
          $error = ApplicationError::fromException($exception);
          $this->request->addError($error);
          $this->response->addError($error);
          $this->request->setAction('failure');
          $this->response->setAction('failure');
          $this->response->setStatus($error->getStatusCode());
          ObjectFactory::getInstance('actionMapper')->processAction($this->request, $this->response);
          return;
        }
      }
      throw $exception;
    }
    catch (\Exception $ex) {
      self::$logger->error($ex->getMessage()."\n".$ex->getTraceAsString());
    }
  }
908706
private function processExceptionContext(Exception $exception, array $context)
    {
        foreach($this->getExceptionHandlerMethods($exception) as $method) {
            $context = $this->{$method->getName()}($exception, $context);
        }

        return $context;
    }
936616
public function transform(Exception $exception)
    {
        if ($exception instanceof ModelNotFoundException) {
            $exception = new NotFoundHttpException($exception->getMessage(), $exception, $exception->getCode());
        }

        return $exception;
    }
738101
private function createResponseFromOAuthException(OAuth2Exception $exception): ResponseInterface
    {
        $payload = [
            'error' => $exception->getCode(),
            'error_description' => $exception->getMessage(),
        ];

        return new Response\JsonResponse($payload, 400);
    }
372206
protected function _method(Exception $exception)
    {
        $exception = $this->_unwrap($exception);
        list(, $baseClass) = namespaceSplit(get_class($exception));

        if (substr($baseClass, -9) === 'Exception') {
            $baseClass = substr($baseClass, 0, -9);
        }

        $method = Inflector::variable($baseClass) ?: 'error500';

        return $this->method = $method;
    }
395440
private static function isConnectionError(\GuzzleHttp\Exception\RequestException $exception = null)
    {
        return $exception instanceof \GuzzleHttp\Exception\ConnectException;
    }
292029
protected function editActionException(\Exception $e)
    {
        $this->handleException($e);
        $id = $this->get('request')->get($this->admin->getIdParameter());
        $object = $this->admin->getObject($id);

        return $this->redirectTo($object);
    }
66367
public function transform(Exception $exception)
    {
        if ($exception instanceof ExceptionInterface) {
            $exception = new BadRequestHttpException($exception->getMessage());
        }

        return $exception;
    }
814484
protected function isGuzzleException(Exception $e)
    {
        return (    is_a($e, GuzzleException\BadResponseException::class)
                ||  is_a($e, GuzzleException\ClientException::class)
                ||  is_a($e, GuzzleException\ConnectException::class)
                ||  is_a($e, GuzzleException\RequestException::class)
                ||  is_a($e, GuzzleException\SeekException::class)
                ||  is_a($e, GuzzleException\ServerException::class)
                ||  is_a($e, GuzzleException\TooManyRedirectsException::class)
                ||  is_a($e, GuzzleException\TransferException::class)
        );
    }