show url and HTTP method in exception
[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
26         throw new Exception(
27             'Error in elasticsearch communication at '
28             . $this->getMethod() . ' ' . (string) $this->getUrl()
29             . ' (status code ' . $res->getStatus() . '): '
30             . $error
31         );
32     }
33 }
34
35 ?>