Skip to content
This repository was archived by the owner on Nov 15, 2022. It is now read-only.

Commit edc44c7

Browse files
committed
Add error messages returned from InfluxDB to curl driver exceptions
This provides more useful error messages than just the HTTP status code, if available (especially useful in case of bad requests)
1 parent bf3415f commit edc44c7

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/InfluxDB/Driver/Curl.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class Curl implements DriverInterface, QueryDriverInterface
2929
/** @var array */
3030
protected $lastRequestInfo;
3131

32+
/** @var mixed */
33+
protected $lastResponse;
34+
3235
/**
3336
* Build the Curl driver from a dsn
3437
* Examples:
@@ -124,7 +127,13 @@ public function isSuccess()
124127
$statusCode = $this->lastRequestInfo['http_code'];
125128

126129
if (!in_array($statusCode, [200, 204], true)) {
127-
throw new Exception('Request failed with HTTP Code ' . $statusCode);
130+
$json = json_decode($this->lastResponse);
131+
132+
if (json_last_error() == JSON_ERROR_NONE && isset($json->error)) {
133+
throw new Exception('Request failed with HTTP Code ' . $statusCode . ': ' . $json->error);
134+
} else {
135+
throw new Exception('Request failed with HTTP Code ' . $statusCode);
136+
}
128137
}
129138

130139
return true;
@@ -152,18 +161,17 @@ protected function execute($url, $curlOptions = [])
152161
curl_setopt($ch, CURLOPT_URL, $this->dsn . '/' . $url);
153162
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
154163

155-
$result = curl_exec($ch);
164+
$this->lastResponse = curl_exec($ch);
156165
$this->lastRequestInfo = curl_getinfo($ch);
157166

158-
if ($result === false) {
167+
if ($this->lastResponse === false) {
159168
// in case of total failure - socket/port is closed etc
160169
throw new Exception('Request failed! curl_errno: ' . curl_errno($ch));
161170
}
162171

163-
164172
curl_close($ch);
165173

166-
return $result;
174+
return $this->lastResponse;
167175
}
168176

169177
/**
@@ -203,4 +211,4 @@ public function getDsn()
203211
{
204212
return $this->dsn;
205213
}
206-
}
214+
}

tests/unit/Driver/CurlTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,23 @@ public function testIsSuccessThrowsExceptionOnHttpError()
217217
$driver->isSuccess();
218218
}
219219

220+
/**
221+
* @expectedException \InfluxDB\Driver\Exception
222+
* @expectedExceptionMessage Request failed with HTTP Code 401: authorization failed
223+
*/
224+
public function testIsSuccessThrowsExceptionWithResponseErrorMessage()
225+
{
226+
$driver = new Curl('http://localhost:8086');
227+
228+
static::$MOCK_INFO = ['http_code' => 401];
229+
static::$MOCK_RESPONSE = '{"error":"authorization failed"}';
230+
$driver->setParameters(['url' => 'write?something']);
231+
232+
$driver->write(['data']);
233+
234+
$driver->isSuccess();
235+
}
236+
220237
/**
221238
* @expectedException \InfluxDB\Driver\Exception
222239
* @expectedExceptionMessage Request failed! curl_errno: 999

0 commit comments

Comments
 (0)