diff --git a/CHANGELOG.md b/CHANGELOG.md index c4b47b1d..ac51e576 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## 3.5.0 [unreleased] +1. [#149](https://github.com/influxdata/influxdb-client-php/issues/149): Throw exception on empty field + ## 3.4.0 [2023-07-28] ### Bug Fixes diff --git a/src/InfluxDB2/Point.php b/src/InfluxDB2/Point.php index f6039a79..7a366cc2 100644 --- a/src/InfluxDB2/Point.php +++ b/src/InfluxDB2/Point.php @@ -114,9 +114,10 @@ public function time($time, $precision = null): Point return $this; } - /** If there is no field then return null. + /** If there is no field then throw an exception. * * @return string representation of the point + * @throws \InvalidArgumentException when no field is provided */ public function toLineProtocol() { @@ -135,7 +136,7 @@ public function toLineProtocol() $fields = $this->appendFields(); if ($this->isNullOrEmptyString($fields)) { - return null; + throw new \InvalidArgumentException("Failed to write Point: At least one field is required."); } $lineProtocol .= $fields; diff --git a/tests/PointTest.php b/tests/PointTest.php index cde6210c..241dc427 100644 --- a/tests/PointTest.php +++ b/tests/PointTest.php @@ -6,6 +6,7 @@ use DateTimeImmutable; use InfluxDB2\Model\WritePrecision; use InfluxDB2\Point; +use InvalidArgumentException; use PHPUnit\Framework\TestCase; class PointTest extends TestCase @@ -262,11 +263,13 @@ public function testWithoutTags() public function testWithoutFields() { + $this->expectException(\InvalidArgumentException::class); + $point = Point::measurement('h2o') ->addTag('location', 'europe') ->time(123); - $this->assertNull($point->toLineProtocol()); + $point->toLineProtocol(); } public function testFromArrayWithoutName()