show url and HTTP method in exception
[phorkie.git] / src / phorkie / Database / Adapter / Elasticsearch / HTTPRequest.php
index 2d74a4e03470213a6d837172cda06fa08193e0e6..24487bcd554d21035154e383f4d369a68987e67b 100644 (file)
@@ -3,22 +3,32 @@ 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) {
-            $js = json_decode($res->getBody());
-            if (isset($js->error)) {
-                $error = $js->error;
-            } else {
-                $error = $res->getBody();
-            }
-            throw new Exception(
-                'Error in elasticsearch communication: ' . $error
-            );
+        if ($mainCode === 2) {
+            return $res;
+        }
+
+        if ($this->allow404 && $res->getStatus() == 404) {
+            return $res;
         }
-        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
+        );
     }
 }