4cc02d6fe6581746e1a62124fd038d46f17af09e
[phorkie.git] / src / phorkie / Database / Adapter / Elasticsearch / HTTPRequest.php
1 <?php
2 namespace phorkie;
3
4 class Database_Adapter_Elasticsearch_HTTPRequest extends \HTTP_Request2
5 {
6     public $allow404 = false;
7
8     public function send()
9     {
10         $res = parent::send();
11         $mainCode = intval($res->getStatus() / 100);
12         if ($mainCode === 2) {
13             return $res;
14         }
15
16         if ($this->allow404 && $res->getStatus() == 404) {
17             return $res;
18         }
19         $js = json_decode($res->getBody());
20         if (isset($js->error)) {
21             $error = $js->error;
22         } else {
23             $error = $res->getBody();
24         }
25         throw new Exception(
26             'Error in elasticsearch communication'
27             . '(status code ' . $res->getStatus() . '): '
28             . $error
29         );
30     }
31 }
32
33 ?>