A factory for creating the appropriate exception based on the response from Telegram.

@param TelegramResponse $response The response that threw the exception.

@return TelegramResponseException
169978
public static function create(TelegramResponse $response)
    {
        $data = $response->getDecodedBody();

        $code = null;
        $message = null;
        if (isset($data['ok'], $data['error_code']) && $data['ok'] === false) {
            $code = $data['error_code'];
            $message = isset($data['description']) ? $data['description'] : 'Unknown error from API.';
        }

        // Others
        throw new static($response, new TelegramOtherException($message, $code));
    }
97913
private function buildResponse($event, $response) {
        $success = !$response->hasErrors();
        $duplicate = ($response->code == 409);
        $statusCode = $response->code;
        $errorMessage = null;
        if ($response->code == 401) {
            $errorMessage = 'Unauthorised. Please check your Project Id and API Key';
        }
        if ($response->body != null && $response->body->errorMessage != null) {
            $errorMessage = $response->body->errorMessage;
        }
        return new Response($success, $duplicate, $statusCode, $errorMessage, $event);
    }
1111731
protected function parseRep($response)
    {
        if ($this->format == 'json') {
            $resp = json_decode($response);

            if (false !== $resp) {
                $resp = current($resp);
            }
        }

        elseif ($this->format == 'xml') {
            $resp = @simplexml_load_string($response);
        }

        else {
            throw new Exception("format错误...");
        }

        return $resp;
    }
85893
final protected function createApiException(ResponseInterface $response, $data, $code = null, $message = null)
    {
        $e = new ApiException(get_class($this).' #'.$code.': '.$message);
        $e->setResponse($response);
        $e->setData($data);
        $e->setErrorCode($code);
        $e->setErrorMessage($message);

        return $e;
    }
855300
private function parseExceptionForErrorMessages(Exception $exception)
    {
        if (! $exception instanceof BadResponseException) {
            return new ApiException($exception->getMessage(), 0, $exception);
        }

        $response = $exception->getResponse();

        if (null === $response) {
            return new ApiException('Response is NULL.', 0, $exception);
        }

        Psr7\rewind_body($response);
        $responseBody = $response->getBody()->getContents();
        $decodedResponseBody = json_decode($responseBody, true);

        if (null !== $decodedResponseBody && isset($decodedResponseBody['error']['message']['value'])) {
            $errorMessage = $decodedResponseBody['error']['message']['value'];
        } else {
            $errorMessage = $responseBody;
        }

        $this->checkWhetherRateLimitHasBeenReached($response, $errorMessage);

        return new ApiException('Error ' . $response->getStatusCode() . ': ' . $errorMessage, $response->getStatusCode(), $exception);
    }
514464
public static function clientException(ResponseInterface $response, $data)
    {
        return static::fromResponse(
            $response,
            isset($data['message']) ? $data['message'] : json_encode($data)
        );
    }
427977
protected function createResponse($response, Exception $e)
    {
        $response = new HttpResponse($response->getContent(), $response->getStatusCode(), $response->headers->all());

        return $response->withException($e);
    }
1338174
public function parseResponse($response)
    {
        $this->responseRaw = $response;

        try {
            $parser = new \Genesis\Parser('xml');
            $parser->skipRootNode();
            $parser->parseDocument($response);

            $this->responseObj = $parser->getObject();
        } catch (\Exception $e) {
            throw new \Genesis\Exceptions\InvalidResponse(
                $e->getMessage(),
                $e->getCode()
            );
        }

        // Apply per-field transformations
        $this->transform([$this->responseObj]);

        if (isset($this->responseObj->status)) {
            $state = new Constants\Transaction\States($this->responseObj->status);

            if (!$state->isValid()) {
                throw new \Genesis\Exceptions\InvalidArgument(
                    'Unknown transaction status',
                    isset($this->responseObj->code) ? $this->responseObj->code : 0
                );
            }

            if ($state->isError() && !$this->suppressReconciliationException()) {
                throw new \Genesis\Exceptions\ErrorAPI(
                    $this->responseObj->message,
                    isset($this->responseObj->code) ? $this->responseObj->code : 0
                );
            }
        }
    }
791848
public function translateResponseToException(\Amp\Artax\Response $response) {
        $status = $response->getStatus();
        if ($status < 200 || $status >= 300) {
            return new BadResponseException(
                "Status $status is not treated as OK.",
                $response
            );
        }

        return null;
    }
1215640
protected function decodeBody(Response $response)
    {
        if ($response->isServerError()) {
            throw new Exception\ClientException(sprintf(
                "'%s' provider encountered a '%s' error while querying '%s'",
                $this->getIdentifier(),
                $response->getReasonPhrase(),
                $this->uri
            ));
        }

        $body = Json::decode($response->getBody());

        if ($response->isClientError()) {
            throw new Exception\ClientException($body->meta->error_message, $response->getStatusCode());
        }

        return $body->data;
    }
1065864
private static function handleErrors($response)
    {
        switch ($response->getStatusCode()) {
            case 200:
                return json_decode($response->getBody());
                break;
            case 401:
                throw new UnauthorizedException('Unauthorized (Invalid API key or insufficient permissions)');
                break;
            case 404:
                throw new NotFoundException('Object not found within your account scope');
                break;
            case 406:
                throw new InvalidFormatException('Requested format is not supported - request JSON or XML only');
                break;
            case 422:
                throw new ValidationException('Validation error(s) for create or update method');
                break;
            case 500:
                throw new ServerException('Something went wrong on Challonge\'s end');
                break;
            default:
                $decodedResponse = json_decode($response->getBody());
                throw new UnexpectedErrorException($decodedResponse);
                break;
        }
    }
1171008
public static function create(
        Request $request,
        Response $response = null,
        Exception $previous = null
    ) {
        if (!$response) {
            return new self('Error completing request', $request, null, $previous);
        }

        $code = $response->getStatusCode();

        switch ($code[0]) {
            case '4':
                $message = 'Client Error';
                break;
            case '5':
                $message = 'Server Error';
                break;
            default:
                $message = 'Unknown Error';
                break;
        }

        return new self($message, $request, $response, $previous);
    }
378693
protected function handleErrorResponse(Response $response)
    {
        $exception = $response->exception;

        $error = new MessageBag([
            'type'    => get_class($exception),
            'message' => $exception->getMessage(),
            'file'    => $exception->getFile(),
            'line'    => $exception->getLine(),
        ]);

        return back()->withInput()->withErrors($error, 'exception');
    }
341515
private function unserializeResponse($response)
    {
        $result = unserialize($response);
        if ($response !== 'b:0;' && $result === false) {
            throw new \Exception(substr($response, 0, 5000));
        }

        return $result;
    }
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));
    }
187609
public static function clientException(ResponseInterface $response, $data)
    {
        return new static($response->getReasonPhrase(), $response->getStatusCode(), (string) $response->getBody());
    }
749041
protected function handleErrorResponse( ResponseInterface $response ){

        $body = json_decode($response->getBody(), true);

        $message = "HTTP:{$response->getStatusCode()}";

        throw new Exception\ApiException( $message, $response->getStatusCode(), $body, $response );

    }