1<?php
2
3namespace Elastica\Exception\Connection;
4
5use Elastica\Exception\ConnectionException;
6use Elastica\Request;
7use Elastica\Response;
8
9/**
10 * Connection exception.
11 *
12 * @author Nicolas Ruflin <spam@ruflin.com>
13 */
14class HttpException extends ConnectionException
15{
16    /**
17     * Error code / message.
18     *
19     * @var int Error code / message
20     */
21    protected $_error = 0;
22
23    /**
24     * Construct Exception.
25     *
26     * @param int $error
27     */
28    public function __construct($error, ?Request $request = null, ?Response $response = null)
29    {
30        $this->_error = $error;
31
32        $message = $this->getErrorMessage($this->getError());
33        parent::__construct($message, $request, $response);
34    }
35
36    /**
37     * Returns the error message corresponding to the error code
38     * cUrl error code reference can be found here {@link http://curl.haxx.se/libcurl/c/libcurl-errors.html}.
39     *
40     * @param int $error Error code
41     *
42     * @return string Error message
43     */
44    public function getErrorMessage(int $error): string
45    {
46        switch ($error) {
47            case \CURLE_UNSUPPORTED_PROTOCOL:
48                return 'Unsupported protocol';
49            case \CURLE_FAILED_INIT:
50                return 'Internal cUrl error?';
51            case \CURLE_URL_MALFORMAT:
52                return 'Malformed URL';
53            case \CURLE_COULDNT_RESOLVE_PROXY:
54                return "Couldn't resolve proxy";
55            case \CURLE_COULDNT_RESOLVE_HOST:
56                return "Couldn't resolve host";
57            case \CURLE_COULDNT_CONNECT:
58                return "Couldn't connect to host, Elasticsearch down?";
59            case \CURLE_OPERATION_TIMEOUTED:
60                return 'Operation timed out';
61        }
62
63        return 'Unknown error:'.$error;
64    }
65
66    /**
67     * Return Error code / message.
68     *
69     * @return int Error code / message
70     */
71    public function getError(): int
72    {
73        return $this->_error;
74    }
75}
76