1<?php
2
3namespace Elastica\Exception;
4
5use Elastica\Request;
6use Elastica\Response;
7
8/**
9 * Response exception.
10 *
11 * @author Nicolas Ruflin <spam@ruflin.com>
12 */
13class ResponseException extends \RuntimeException implements ExceptionInterface
14{
15    /**
16     * @var Request Request object
17     */
18    protected $_request;
19
20    /**
21     * @var Response Response object
22     */
23    protected $_response;
24
25    /**
26     * Construct Exception.
27     */
28    public function __construct(Request $request, Response $response)
29    {
30        $this->_request = $request;
31        $this->_response = $response;
32        parent::__construct($response->getErrorMessage());
33    }
34
35    /**
36     * Returns request object.
37     *
38     * @return Request Request object
39     */
40    public function getRequest(): Request
41    {
42        return $this->_request;
43    }
44
45    /**
46     * Returns response object.
47     *
48     * @return Response Response object
49     */
50    public function getResponse(): Response
51    {
52        return $this->_response;
53    }
54
55    /**
56     * Returns elasticsearch exception.
57     *
58     * @deprecated since version 7.1.0, use the "getResponse()::getFullError()" method instead.
59     */
60    public function getElasticsearchException(): ElasticsearchException
61    {
62        \trigger_deprecation('ruflin/elastica', '7.1.0', 'The "%s()" method is deprecated, use "getResponse()::getFullError()" instead. It will be removed in 8.0.', __METHOD__);
63
64        $response = $this->getResponse();
65
66        return new ElasticsearchException($response->getStatus(), $response->getErrorMessage());
67    }
68}
69