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     * @param Request  $request
28     * @param Response $response
29     */
30    public function __construct($error, Request $request = null, Response $response = null)
31    {
32        $this->_error = $error;
33
34        $message = $this->getErrorMessage($this->getError());
35        parent::__construct($message, $request, $response);
36    }
37
38    /**
39     * Returns the error message corresponding to the error code
40     * cUrl error code reference can be found here {@link http://curl.haxx.se/libcurl/c/libcurl-errors.html}.
41     *
42     * @param int $error Error code
43     *
44     * @return string Error message
45     */
46    public function getErrorMessage(int $error): string
47    {
48        switch ($error) {
49            case CURLE_UNSUPPORTED_PROTOCOL:
50                return 'Unsupported protocol';
51            case CURLE_FAILED_INIT:
52                return 'Internal cUrl error?';
53            case CURLE_URL_MALFORMAT:
54                return 'Malformed URL';
55            case CURLE_COULDNT_RESOLVE_PROXY:
56                return "Couldn't resolve proxy";
57            case CURLE_COULDNT_RESOLVE_HOST:
58                return "Couldn't resolve host";
59            case CURLE_COULDNT_CONNECT:
60                return "Couldn't connect to host, Elasticsearch down?";
61            case CURLE_OPERATION_TIMEOUTED:
62                return 'Operation timed out';
63        }
64
65        return 'Unknown error:'.$error;
66    }
67
68    /**
69     * Return Error code / message.
70     *
71     * @return int Error code / message
72     */
73    public function getError(): int
74    {
75        return $this->_error;
76    }
77}
78