aboutsummaryrefslogtreecommitdiff
path: root/src/phorkie/Database/Adapter/Elasticsearch/HTTPRequest.php
blob: 24487bcd554d21035154e383f4d369a68987e67b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
namespace phorkie;

class Database_Adapter_Elasticsearch_HTTPRequest extends \HTTP_Request2
{
    public $allow404 = false;

    public function send()
    {
        $res = parent::send();
        $mainCode = intval($res->getStatus() / 100);
        if ($mainCode === 2) {
            return $res;
        }

        if ($this->allow404 && $res->getStatus() == 404) {
            return $res;
        }
        $js = json_decode($res->getBody());
        if (isset($js->error)) {
            $error = $js->error;
        } else {
            $error = $res->getBody();
        }

        throw new Exception(
            'Error in elasticsearch communication at '
            . $this->getMethod() . ' ' . (string) $this->getUrl()
            . ' (status code ' . $res->getStatus() . '): '
            . $error
        );
    }
}

?>