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     * @param Request  $request
29     * @param Response $response
30     */
31    public function __construct(Request $request, Response $response)
32    {
33        $this->_request = $request;
34        $this->_response = $response;
35        parent::__construct($response->getErrorMessage());
36    }
37
38    /**
39     * Returns request object.
40     *
41     * @return Request Request object
42     */
43    public function getRequest(): Request
44    {
45        return $this->_request;
46    }
47
48    /**
49     * Returns response object.
50     *
51     * @return Response Response object
52     */
53    public function getResponse(): Response
54    {
55        return $this->_response;
56    }
57
58    /**
59     * Returns elasticsearch exception.
60     *
61     * @return ElasticsearchException
62     */
63    public function getElasticsearchException(): ElasticsearchException
64    {
65        $response = $this->getResponse();
66
67        return new ElasticsearchException($response->getStatus(), $response->getErrorMessage());
68    }
69}
70